Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Visual Basic 2005
How to Program, 3/e

ISBN:
0-13-186900-0
© 2005
pages: ~1500

Pre-order!
Amazon logo

This is the second in a series of three articles that shows how to declare and use generic methods in the C# programming language. In Part 1, we introduced the concept of generics. In this article, we present an example using overloaded methods to motivate the need for generic methods. In Part 3, we reimplement the overloaded methods from the example in this article using a single generic method. These articles are intended for students who are already familiar with Visual Basic and for Visual Basic developers.
Download the code examples for this tutorial here.

[Note: This three-part tutorial is an excerpt (Sections 25.1-3) of Chapter 25, Generics, from our forthcoming textbook Visual Basic 2005 How to Program, 3/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., Visual Basic 2005 How to Program, ©2005. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

25.2 Motivation for Generic Methods

Overloaded methods are often used to perform similar operations on different types of data. To motivate the concepte of generic methods, let's begin with an example (Fig. 25.1) that contains three overloaded PrintArray methods (lines 19–25, 28–34 and 37–43). These methods display the elements of an Integer array, a Double array and a Char array, respectively. In Section 25.3, we reimplement this program more concisely and elegantly using a single generic method.

Fig. 25.1 | Displaying arrays of different types using overloaded methods.
1   ' Fig. 25.1: OverloadedMethods.vb
2   ' Using overloaded methods to print arrays of different types.
3   Module OverloadedMethods
4      Sub Main()
5          ' create arrays of Integer, Double and Char types
6         Dim integerArray As Integer() = {1, 2, 3, 4, 5, 6}
7         Dim doubleArray As Double() = {1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7}
8         Dim charArray As Char() = {"H"c, "E"c, "L"c, "L"c, "O"c}
9   
10         Console.WriteLine("Array integerArray contains:")
11         PrintArray(integerArray) ' pass an Integer array argument
12          Console.WriteLine("Array doubleArray contains:")
13         PrintArray(doubleArray) ' pass a Double array argument
14         Console.WriteLine("Array charArray contains:")
15         PrintArray(charArray) ' pass a Char array
16      End Sub ' Main
17   
18       ' output Integer array
19      Sub PrintArray(ByVal inputArray() As Integer)
20         For Each element As Integer In inputArray
21            Console.Write(element.ToString() & " ")
22         Next element
23   
24         Console.WriteLine(vbCrLf)
25      End Sub ' PrintArray
26   
27       ' output Double array
28      Sub PrintArray(ByVal inputArray() As Double)
29         For Each element As Double In inputArray
30            Console.Write(element.ToString() & " ")
31         Next element
32   
33         Console.WriteLine(vbCrLf)
34      End Sub ' PrintArray
35   
36       ' output Char array
37      Sub PrintArray(ByVal inputArray() As Char)
38         For Each element As Char In inputArray
39            Console.Write(element.ToString() & " ")
40         Next element
41   
42         Console.WriteLine(vbCrLf)
43      End Sub ' PrintArray
44    End Module ' OverloadedMethods

Array integerArray contains:
1 2 3 4 5 6

Array doubleArray contains:
1.1 2.2 3.3 4.4 5.5 6.6 7.7

Array charArray contains:
H E L L O

The program begins by declaring and initializing three arrays—six-element Integer array integerArray (line 6), seven-element Double array doubleArray (line 7) and five-element Char array charArray (line 8). Then lines 10–15 output the arrays.

When the compiler encounters a method call, it attempts to locate a method declaration that has the same method name and parameters that match the argument types in the method call. In this example, each PrintArray call exactly matches one of the PrintArray method declarations. For example, line 11 calls PrintArray with integerArray as its argument. At compile time, the compiler determines argument integerArray's type (i.e., Integer()), attempts to locate a method named PrintArray that specifies a single Integer() parameter (which it finds in lines 19–25) and sets up a call to that method. Similarly, when the compiler encounters the PrintArray call in line 13, it determines argument doubleArray's type (i.e., Double()), then attempts to locate a method named PrintArray that specifies a single Double() parameter (which it finds in lines 28–34) and sets up a call to that method. Finally, when the compiler encounters the PrintArray call in line 15, it determines argument charArray's type (i.e., Char()), then attempts to locate a method named PrintArray that specifies a single Char() parameter (which it finds in lines 37–43) and sets up a call to that method.

Study each PrintArray method. Note that the array element type (Integer, Double or Char) appears in two locations in each method—the method header (lines 19, 28 and 37) and the For Each statement header (lines 20, 29 and 38). If we replace the element type in each method with a generic name—we chose E to represent the "element" type—then all three methods would look like the one in Fig. 25.2. It appears that if we can replace the array element type in each of the three methods with a single "generic type parameter," then we should be able to declare one PrintArray method that can display the elements of any array. The method in Fig. 25.2 will not compile because its syntax is not correct—we declare a generic PrintArray method with the proper syntax in Fig. 25.3.

Fig. 25.2 | PrintArray method in which actual type names are replaced by convention with the generic name E (for "element"). [Note: This code will not compile.]
1      Public Sub PrintArray(ByVal inputArray() As E)
2         For Each element As E In inputArray
3            Console.Write(element.ToString() & " ")
4         Next element
5   
6         Console.WriteLine(vbCrLf)
7      End Sub ' PrintArray

Tutorials in This Series:
Introduction to Generics in Visual Basic
Motivation for Generic Methods (You are here.)
Generic Method Implementation

Tutorial Index