7.18. Optional ParametersMethods can have optional parameters. Declaring a parameter as optional allows the calling method to vary the number of arguments to pass. Optional parameters specify a default value that is assigned to the parameter if the optional argument is not passed. Overloaded methods are generally more flexible than methods with optional parameters. For instance, you can specify different return types for overloaded methods. You can create methods with one or more optional parameters. All optional parameters, however, must be placed to the right of the method's non-optional parameters. Common Programming Error 7.10
When a parameter is declared as optional, the caller has the option of passing that particular argument. Optional parameters are specified in the method header with keyword Optional. For example, the method header Sub ExampleMethod(ByVal value1 As Boolean, Optional _ ByVal value2 As Integer = 0) specifies the last parameter as Optional. Any call to ExampleMethod must pass at least one argument (the Boolean), or else a syntax error is generated. If the caller chooses, a second argument (the Integer) can be passed to ExampleMethod. Consider the following calls to ExampleMethod: ExampleMethod() ExampleMethod(True) ExampleMethod(False, 10) The first call to ExampleMethod generates a syntax error because a minimum of one argument is required for this method. The second call to ExampleMethod is valid because one argument (the Boolean) is being passedthe Optional argument, corresponding to parameter value2, is not specified in the method call. The last call to ExampleMethod also is valid: False is passed as the one required argument, and 10 is passed as the Optional argument. In the call that passes only one argument (TRue) to ExampleMethod, parameter value2 defaults to 0, which is the value specified in the method header. Optional parameters must specify a default value, using the equals sign followed by the value. For example, the header for ExampleMethod sets 0 as the default value for value2. Default values can be used only with parameters declared as Optional. Common Programming Error 7.11
The example in Fig. 7.19 demonstrates the use of optional parameters. The program calculates the result of raising a base to an exponent, both of which are specified by the user. Method Power (lines 2333) specifies that its second parameter is Optional. If the user does not specify an exponent, the Optional argument is omitted, and the default parameter value, 2, is used. Figure 7.19. Optional argument demonstration with method Power.
In this example, we use TextBoxes to input data from the user. When the Calculate Power Button is clicked, line 11 determines whether txtPower contains a value. If true (as in Fig. 7.19(a)), the values in the TextBoxes are converted to Integers and passed to Power. Otherwise, txtBase's value is converted to an Integer and passed as the first of two arguments to Power in line 15. An example of this is shown in Fig. 7.19(b), where we clicked the Calculate Power Button without entering an exponent value. The second argument, 2, is provided by the compiler (using the default value of the Optional argument) and is not visible to you in the call. Line 16 displays the value 2 in txtPower, for clarity (Fig. 7.19(c)). |