How to: Overload a Procedure that Takes Optional Parameters
If a procedure has one or more Optional (Visual Basic) parameters, you cannot define an overloaded version matching any of its implicit overloads. For more information, see "Implicit Overloads for Optional Parameters" in Considerations in Overloading Procedures.
One Optional Parameter
To overload a procedure that takes one optional parameter
Write a Sub or Function declaration statement that includes the optional parameter in the parameter list. Do not use the Optional keyword in this overloaded version.
Precede the Sub or Function keyword with the Overloads keyword.
Write the procedure code that should execute when the calling code supplies the optional argument.
Terminate the procedure with the End Sub or End Function statement as appropriate.
Write a second declaration statement that is identical to the first declaration except that it does not include the optional parameter in the parameter list.
Write the procedure code that should execute when the calling code does not supply the optional argument. Terminate the procedure with the End Sub or End Function statement as appropriate.
The following example shows a procedure defined with an optional parameter, an equivalent set of two overloaded procedures, and finally examples of both invalid and valid overloaded versions.
Sub q(ByVal b As Byte, Optional ByVal j As Long = 6) ' The preceding definition is equivalent to the following two overloads. ' Overloads Sub q(ByVal b As Byte) ' Overloads Sub q(ByVal b As Byte, ByVal j As Long) ' Therefore, the following overload is not valid because the signature is already in use. ' Overloads Sub q(ByVal c As Byte, ByVal k As Long) ' The following overload uses a different signature and is valid. Overloads Sub q(ByVal b As Byte, ByVal j As Long, ByVal s As Single)
Multiple Optional Parameters
For a procedure with more than one optional parameter, you normally need more than two overloaded versions. For example, if there are two optional parameters, and the calling code can supply or omit each one independently of the other, you need four overloaded versions, one for each possible combination of supplied arguments.
As the number of optional parameters increases, the complexity of the overloading increases. Unless some combinations of supplied arguments are not acceptable, for N optional parameters you need 2 ^ N overloaded versions. Depending on the nature of the procedure, you might find that the clarity of logic justifies the extra effort of defining all the overloaded versions.
To overload a procedure that takes more than one optional parameter
Determine which combinations of supplied optional arguments are acceptable to the logic of the procedure. An unacceptable combination might arise if one optional parameter depends on another. For example, if one parameter accepts a spouse's name and another accepts the spouse's age, a combination of arguments supplying the age but omitting the name is unacceptable.
For each acceptable combination of supplied optional arguments, write a Sub or Function declaration statement that defines the corresponding parameter list. Do not use the Optional keyword.
In each declaration, precede the Sub or Function keyword with the Overloads keyword.
Following each declaration, write the procedure code that should execute when the calling code supplies an argument list corresponding to that declaration's parameter list.
Terminate each procedure with the End Sub or End Function statement as appropriate.
See Also
Tasks
Troubleshooting Procedures
How to: Define Multiple Versions of a Procedure
How to: Call an Overloaded Procedure
How to: Overload a Procedure that Takes an Indefinite Number of Parameters
Concepts
Procedures in Visual Basic
Procedure Parameters and Arguments
Optional Parameters
Parameter Arrays
Procedure Overloading
Overload Resolution