Section 7.17. Method Overloading


7.17. Method Overloading

Visual Basic provides several ways of allowing methods to have variable sets of parameters. Method overloading allows you to create multiple methods with the same name but different signaturesthat is, different numbers and types of parameters, or with different orderings of the parameters (by type). When an overloaded method is called, the compiler selects the proper method by examining the number, types and order (by type) of the arguments. Often, method overloading is used to create several methods with the same name that perform similar tasks on different types of data.

Good Programming Practice 7.3

Overloading methods that perform closely related tasks can make programs clearer.


The program in Fig. 7.18 uses overloaded method Square to calculate the square of both an Integer and a Double.

Figure 7.18. Syntax error generated from overloaded methods with identical parameter lists and different return types.

  1  ' Fig. 7.18: Overload2.vb  2  ' Using overloaded methods with identical signatures and  3  ' different return types.  4  Module Overload2  5     Sub Main() ' call Square methods with Integer and Double  6        Console.WriteLine("The square of Integer 7 is " & Square(7) & _  7           vbCrLf & "The square of Double 7.5 is " & Square(7.5))  8     End Sub ' Main  9 10     ' method takes a  Double and returns an Integer 11     Function Square(ByVal value As Double) As Integer 12        Return Convert.ToInt32(value ^ 2) 13     End Function ' Square 14 15     ' method takes a  Double and returns a Double 16     Function Square(ByVal value As Double) As Double 17        Return value ^ 2 18     End Function ' Square 19  End Module ' Overload2 

If the compiler looked only at method names during compilation, the code in Fig. 7.18 would be ambiguousthe compiler would not know how to differentiate between the two Square methods. The compiler uses a process known as overload resolution to determine which method to call. This process first searches for all methods that could be used on the basis of the number and type of arguments that are present. Although it might seem that only one method would match, it is important to remember that Visual Basic converts variables as necessary when they are passed as arguments. Once all matching methods are found, the compiler then selects the closest match.

Note that at line 12 we use method Convert.ToInt32, which converts its argument to a value of type Integer. This explicit conversion is needed because the exponent (^) operator expects operands of type Double, and implicitly converts its operands to that type. This version of method Square returns an Integer value, so Convert.ToInt32 performs this conversion. Recall that because we are now using Option Strict, such conversion is required to compile the application.

In Fig. 7.18, the compiler might use the logical name "Square of Integer" for the Square method that specifies an Integer parameter (line 10) and "Square of Double" for the Square method that specifies a Double parameter (line 15). If a method ExampleSub's declaration begins as

 Function ExampleSub(ByVal a As Integer, ByVal b As Double) _    As Integer 


the compiler might use the logical name "ExampleSub of Integer and Double." Similarly, if the parameters are specified as

 Function ExampleSub(ByVal a As Double, ByVal b As Integer) _    As Integer 


the compiler might use the logical name "ExampleSub of Double and Integer." The order of the parameters (by type) is important to the compiler; it considers the preceding two ExampleSub methods to be distinct.

So far, the logical method names used by the compiler have not mentioned the methods' return types. This is because method calls cannot be distinguished by return type. The program in Fig. 7.18 illustrates the syntax error that is generated when two methods have the same signature and different return types. Overloaded methods with different parameter lists can have different return types. Overloaded methods need not have the same number of parameters.

Common Programming Error 7.9

Creating overloaded methods with identical parameter lists and different return types is a compilation error.




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