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.

Figure 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 ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.