Generic Methods


A generic method is a single method that is called not only with conventional parameters, but also with type information that defines the method. Generic methods are far less common than generic types. Due to the extra syntax required to call a generic method, they are also less readable than a normal method.

A generic method may exist in any class or module; it doesn’t need to be contained within a generic type. The primary benefit of a generic method is avoiding the use of CType or DirectCast to convert parameters or return values between different types.

It is important to realize that the type conversion still occurs; generics merely provide an alternative mechanism to use instead of CType or DirectCast.

Without generics, code often uses the Object type. Add the following method to Form1:

  Private Function AreEqual(ByVal a As Object, ByVal b As Object) As Boolean   Return a.Equals(b) End Function 

The problem with this code is that aand bcould be anything. There’s no restriction here, nothing to ensure that they are even the same type. An alternative is to use generics. Add the following method to Form1:

  Public Function AreEqual(Of T)(ByVal a As T, ByVal b As T) As Boolean   Return a.Equals(b) End Function 

Now a and b are forced to be the same type, and that type is specified when the method is invoked.

Add a new Button named btnEqual to Form1 with the following code in its click event:

  Dim result As Boolean ' use normal method result = AreEqual(1, 2) result = AreEqual("one", "two") result = AreEqual(1, "two") ' use generic method result = AreEqual(Of Integer)(1, 2) result = AreEqual(Of String)("one", "two") 'result = AreEqual(Of Integer)(1, "two") 

However, why not just declare the method as a Boolean? This code will probably cause some confusion.

The first three method calls are invoking the normal AreEqual() method. Notice that there’s no problem asking the method to compare an Integer and a String.

The second set of calls looks very odd. At first glance, they look like nonsense to many people. This is because invoking a generic method means providing two sets of parameters to the method, rather than the normal one set of parameters.

The first set of parameters defines the type or types required to define the method. This is much like the list of types you must provide when declaring a variable using a generic class. In this case, you’re specifying that the AreEqual() method will be operating on parameters of type Integer.

The second set of parameters are the conventional parameters that you’d normally supply to a method. What is special in this case is that the types of the parameters are being defined by the first set of parameters. In other words, in the first call, the type is specified to be Integer, so 1 and 2 are valid parameters. In the second call, the type is String, so “one” and “two” are valid. Notice that the third line is commented out. This is because 1 and “two” aren’t the same type, so the compiler won’t compile that line of code.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net