Share via


How to: Overload a Procedure that Takes an Indefinite Number of Parameters (Visual Basic)

If a procedure has a ParamArray (Visual Basic) 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 (Visual Basic).

To overload a procedure that takes a variable number of parameters

  1. 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 (Visual Basic).

  2. 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.

  3. For each acceptable number of supplied values, write a Sub or Function declaration statement that defines the corresponding parameter list. Do not use either the Optional or the ParamArray keyword in this overloaded version.

  4. In each declaration, precede the Sub or Function keyword with the Overloads (Visual Basic) keyword.

  5. Following each declaration, write the procedure code that should execute when the calling code supplies values corresponding to that declaration's parameter list.

  6. Terminate each procedure with the End Sub or End Function statement as appropriate.

Example

The following example shows a procedure defined with a ParamArray (Visual Basic) 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.

Compiling 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 (Visual Basic).

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.

See Also

Tasks

Troubleshooting Procedures (Visual Basic)

How to: Define Multiple Versions of a Procedure (Visual Basic)

How to: Call an Overloaded Procedure (Visual Basic)

How to: Overload a Procedure that Takes Optional Parameters (Visual Basic)

Concepts

Procedures in Visual Basic

Procedure Parameters and Arguments (Visual Basic)

Optional Parameters (Visual Basic)

Parameter Arrays (Visual Basic)

Procedure Overloading (Visual Basic)

Overload Resolution (Visual Basic)