How to: Overload a Procedure that Takes an Indefinite Number of Parameters (Visual Basic)
If a procedure has a ParamArray parameter, you cannot define an overloaded version taking a one-dimensional array for the parameter array. For more information, see "Implicit Overloads for a ParamArray Parameter" in Considerations in Overloading Procedures.
To overload a procedure that takes a variable number of parameters
Ascertain that the procedure and calling code logic benefits from overloaded versions more than from a
ParamArray
parameter. See "Overloads and ParamArrays" in Considerations in Overloading Procedures.Determine which numbers of supplied values the procedure should accept in the variable part of the parameter list. This might include the case of no value, and it might include the case of a single one-dimensional array.
For each acceptable number of supplied values, write a
Sub
orFunction
declaration statement that defines the corresponding parameter list. Do not use either theOptional
or theParamArray
keyword in this overloaded version.In each declaration, precede the
Sub
orFunction
keyword with the Overloads keyword.Following each declaration, write the procedure code that should execute when the calling code supplies values corresponding to that declaration's parameter list.
Terminate each procedure with the
End Sub
orEnd Function
statement as appropriate.
Example
The following example shows a procedure defined with a ParamArray parameter, and then an equivalent set of overloaded procedures.
Sub p(ByVal d As Date, ByVal ParamArray c() As Char)
' The preceding definition is equivalent to the following overloads.
' Overloads Sub p(ByVal d As Date)
' Overloads Sub p(ByVal d As Date, ByVal c() As Char)
' Overloads Sub p(ByVal d As Date, ByVal c1 As Char)
' Overloads Sub p(ByVal d As Date, ByVal c1 As Char, ByVal c2 As Char)
' And so on, with an additional Char argument in each successive overload.
You cannot overload such a procedure with a parameter list that takes a one-dimensional array for the parameter array. However, you can use the signatures of the other implicit overloads. The following declarations illustrate this.
' The following overload is not valid because it takes an array for the parameter array.
' Overloads Sub p(ByVal x As Date, ByVal y() As Char)
' The following overload takes a single value for the parameter array and is valid.
Overloads Sub p(ByVal z As Date, ByVal w As Char)
The code in the overloaded versions does not have to test whether the calling code supplied one or more values for the ParamArray
parameter, or if so, how many. Visual Basic passes control to the version matching the calling argument list.
Compile the code
Because a procedure with a ParamArray
parameter is equivalent to a set of overloaded versions, you cannot overload such a procedure with a parameter list corresponding to any of these implicit overloads. For more information, see Considerations in Overloading Procedures.
.NET Framework Security
Whenever you deal with an array which can be indefinitely large, there is a risk of overrunning some internal capacity of your application. If you accept a parameter array, you should test for the length of the array the calling code passed to it, and take appropriate steps if it is too large for your application.