共用方式為


How to: Overload a Procedure that Takes an Indefinite Number of Parameters 

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

  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.

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

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
How to: Define Multiple Versions of a Procedure
How to: Call an Overloaded Procedure
How to: Overload a Procedure that Takes Optional Parameters

Concepts

Procedures in Visual Basic
Procedure Parameters and Arguments
Optional Parameters
Parameter Arrays
Procedure Overloading
Overload Resolution