Section 25.3. Generic Method Implementation


25.3. Generic Method Implementation

The overloaded methods of Fig. 25.1 can be more compactly and conveniently coded using a single generic method. You can write a single generic method declaration that can be called at different times with arguments of different types. Based on the types of the arguments passed to the generic method, the compiler handles each method call appropriately.

Figure 25.3 reimplements the application of Fig. 25.1 using a single generic Print-Array method (lines 1925). Note that the PrintArray method calls in lines 11, 13 and 15 are identical to those in Fig. 25.1, the outputs of the two applications are identical and the code in Fig. 25.3 is 18 lines shorter than the code in Fig.25.1. As illustrated in Fig. 25.3, generics enable us to create and test our code once, then reuse the code for many different types of data. This demonstrates the expressive power of generics.

Line 19 begins method PrintArray's declaration. All generic method declarations have a type parameter list delimited by parentheses(Of E) in this examplethat follows the method's name. Each type parameter list begins with the keyword Of and contains one or more type parameters separated by commas. A type parameter is an identifier that is used in place of actual type names. The type parameters can be used to declare the return type, the parameter types and the local variable types in a generic method declaration; the type parameters act as placeholders for the types of the arguments passed to the generic method. A generic method's body is declared like that of any other method. Note that the type parameter names throughout the method declaration must match those declared in the type parameter list. For example, line 20 declares element in the For Each statement as type E, which matches the type parameter (E) declared in line 19. Also, a type parameter can be declared only once in the type parameter list but can appear more than once in the method's parameter list. Type parameter names need not be unique among different generic methods.

Common Programming Error 25.1

If you forget to include the type parameter list when declaring a generic method, the compiler will not recognize the type parameter names when they are encountered in the method. This results in compilation errors.


Method PrintArray's type parameter list (line 19) declares type parameter E as the placeholder for the array element type that PrintArray will output. Note that E appears in the method's parameter list as the array element type (line 19). The For Each statement header (line 20) also uses E as the element type. These are the same two locations where the overloaded PrintArray methods of Fig. 25.1 specified Integer, Double or Char as the array element type. The remainder of PrintArray is identical to the version in Fig. 25.1.

Good Programming Practice 25.1

According to msdn.microsoft.com/library/en-us/dndotnet/html/BestPractices.asp, it is recommended that type parameters be specified as individual capital letters. Typically, a type parameter that represents the type of an element in an array (or other collection) is named E for "element" or T for "type."


As in Fig. 25.1, the program in Fig. 25.3 begins by declaring and initializing six-element Integer array integerArray (line 6), seven-element Double array doubleArray (line 7) and five-element Char array charArray (line 8). Then each array is output by calling PrintArray (lines 11, 13 and 15)once with argument integerArray, once with argument doubleArray and once with argument charArray.

When the compiler encounters a method call, such as line 11, it analyzes the set of methods (both non-generic and generic) that might match the method call, looking for a method that matches the call exactly. If there are no exact matches, the compiler picks the best match. If there are no matching methods, or if there is more than one best match, the compiler generates an error. The complete details of method-call resolution can be found in Section 11.8.1 of the Visual Basic Language Specification version 8.0, which can be downloaded from:

http://go.microsoft.com/fwlink/?LinkId=62990

For line 11, the compiler determines that an exact match occurs if the type parameter E in lines 19 and 20 of method PrintArray's declaration is replaced with the type of the elements in the method call's argument integerArray (i.e., Integer). Then the compiler sets up a call to PrintArray with Integer as the type argument for the type parameter E. This is known as the type inferencing process. The same process is repeated for the calls to method PrintArray in lines 13 and 15.

Common Programming Error 25.2

If the compiler cannot find a single non-generic or generic method declaration that is a best match for a method call, or if there are multiple best matches, a compilation error occurs.


You can also use explicit type arguments to indicate the exact type that should be used to call a generic function. For example, line 11 could be written as

 PrintArray(Of Integer)(integerArray) ' call Integer version 


In the preceding method call, the first set of parentheses contains the explicit type argument Integer that should be used to replace type parameter E in lines 19 and 20 of method PrintArray's declaration.

The compiler also determines whether the operations performed on the method's type parameters can be applied to elements of the type stored in the array argument. The only operation performed on the array elements in this example is to output the String representation of the elements. Line 21 calls ToString on the current array element being processed. Since all objects have a ToString method, the compiler is satisfied that line 21 performs a valid operation for any array element.

By declaring PrintArray as a generic method in Fig. 25.3, we eliminated the need for the overloaded methods of Fig. 25.1, saving 18 lines of code and creating a reusable method that can output the string representations of the elements of any array, not just arrays of Integer, Double or Char elements.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

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