Udostępnij za pośrednictwem


How to: Define a Procedure with an Indefinite Number of Parameters (Visual Basic)

You can declare a parameter array as the last entry in a procedure's parameter list. This allows the procedure to accept a set of values for that parameter, instead of just a single value. You do not have to know the number of values in the set when you define the procedure. The set is determined individually by each call to the procedure, and each call can pass a different number of values.

For more information, see Parameter Arrays (Visual Basic).

To define a procedure that can accept an indefinite number of values for its last parameter

  1. In the procedure declaration, define the parameter list in the normal way. All parameters except the last one must be required (not Optional (Visual Basic)).

  2. Precede the last parameter name with the keywords ByVal ParamArray. This parameter is automatically optional. Do not include the Optional keyword.

  3. Follow the parameter array name with an empty pair of parentheses.

  4. Follow the empty parentheses with the usual As clause.

  5. Do not follow the As clause with a default value. The parameter array's default value is automatically an empty one-dimensional array of the data type you specified in the As clause.

Working with the Parameter Array Values

The code within the procedure must treat the parameter array as a one-dimensional array, each element of which is the same data type as the ParamArray data type.

To access one of the values of a parameter array

  1. In the procedure code, determine the length of the array passed to the parameter array by calling the UBound function on the parameter array name.

  2. In an executable statement in the procedure code, follow the parameter array name with a subscript in parentheses. This subscript should be between 0 and the upper bound returned by UBound.

Security noteSecurity Note

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 from the calling code, you should test its length and take appropriate steps if it is too large for your application.

Example

The following example defines a procedure with a parameter array, and outputs the values of all the array elements passed to the parameter array.

Sub studentScores(ByVal name As String, ByVal ParamArray scores() As String)
    Debug.WriteLine("Scores for " & name & ":" & vbCrLf)
    ' Use UBound to determine largest subscript of the array.
    For i As Integer = 0 To UBound(scores, 1)
        Debug.WriteLine("Score " & i & ": " & scores(i))
    Next i
End Sub

The following examples show typical calls to studentScores.

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)

Compiling the Code

Make sure the ParamArray is the last parameter in the parameter list, and that none of the preceding parameters is declared Optional.

See Also

Tasks

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

Reference

ByVal (Visual Basic)

ParamArray (Visual Basic)

Concepts

Procedure Parameters and Arguments (Visual Basic)

Passing Arguments by Value and by Reference (Visual Basic)

Passing Arguments by Position and by Name (Visual Basic)

Optional Parameters (Visual Basic)

Procedure Overloading (Visual Basic)

Type Checking in Visual Basic

Other Resources

Arrays in Visual Basic