Udostępnij za pośrednictwem


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

A procedure can declare the last entry in its parameter list to be a parameter array. This allows it to accept an indefinite number of values for that parameter, instead of just a single value.

For more information, see Parameter Arrays.

To call a procedure with a parameter array and omit the corresponding argument

  1. Write the procedure call in the normal way. The parameter array must be the last argument.

  2. Terminate the argument list with the next-to-last argument. The parameter array is optional, and all the preceding parameters must be required.

    -or-

    Supply the Nothing keyword as the argument for the parameter array.

  3. Visual Basic passes an empty one-dimensional array to the procedure for the parameter array.

To call a procedure with a parameter array and supply a list of arguments

  1. Write the procedure call in the normal way. The parameter array must be the last argument.

  2. Supply any number of arguments, separated by commas, for the parameter array. The data type of each argument must be implicitly convertible to the ParamArray element type.

  3. Visual Basic passes a one-dimensional array to the procedure, containing all the values you supplied.

To call a procedure with a parameter array and supply an array of arguments

  1. Write the procedure call in the normal way. The parameter array must be the last argument.

  2. For the parameter array, supply a one-dimensional array with the same element type as the parameter array's element type.

  3. Visual Basic passes your array to the procedure.

Example

The following examples show typical calls to the studentScores procedure defined in How to: Define a Procedure with an Indefinite Number of Parameters.

Call studentScores("George")


...


Call studentScores("Anne", "10", "26", "32", "15", "22", "24", "16")
Call studentScores("Mary", "High", "Low", "Average", "High")
Dim JohnScores() As String = {"35", "Absent", "21", "30"}
Call studentScores("John", JohnScores)

The first call omits the parameter array altogether and supplies only the required first argument. The studentScores procedure treats this call as passing an empty array.

The second and third calls supply argument lists of different lengths to the parameter array. Each such list is passed as an array of values.

The fourth call passes an array to the parameter array.

See Also

Concepts

Procedure Parameters and Arguments

Passing Arguments by Value and by Reference

Passing Arguments by Position and by Name

Optional Parameters

Procedure Overloading

Type Checking in Visual Basic

Reference

Optional (Visual Basic)

ParamArray

ByVal

UBound Function (Visual Basic)