7.17. Method OverloadingVisual 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
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.
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
|