Muokkaa

Jaa


Parameter Arrays (Visual Basic)

Usually, you cannot call a procedure with more arguments than the procedure declaration specifies. When you need an indefinite number of arguments, you can declare a parameter array, which allows a procedure to accept an array of values for a parameter. You do not have to know the number of elements in the parameter array when you define the procedure. The array size is determined individually by each call to the procedure.

Declaring a ParamArray

You use the ParamArray keyword to denote a parameter array in the parameter list. The following rules apply:

  • A procedure can define only one parameter array, and it must be the last parameter in the procedure definition.

  • The parameter array must be passed by value. It is good programming practice to explicitly include the ByVal keyword in the procedure definition.

  • The parameter array is automatically optional. Its default value is an empty one-dimensional array of the parameter array's element type.

  • All parameters preceding the parameter array must be required. The parameter array must be the only optional parameter.

Calling a ParamArray

When you call a procedure that defines a parameter array, you can supply the argument in any one of the following ways:

  • Nothing — that is, you can omit the ParamArray argument. In this case, an empty array is passed to the procedure. If you explicitly pass the Nothing keyword, a null array is passed to the procedure and may result in a NullReferenceException if the called procedure does not check for this condition.

  • A list of an arbitrary number of arguments, separated by commas. The data type of each argument must be implicitly convertible to the ParamArray element type.

  • An array with the same element type as the parameter array's element type.

In all cases, the code within the procedure treats the parameter array as a one-dimensional array with elements of the same data type as the ParamArray data type.

Important

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 size of the array that the calling code passed to it. Take appropriate steps if it is too large for your application. For more information, see Arrays.

Example

The following example defines and calls the function calcSum. The ParamArray modifier for the parameter args enables the function to accept a variable number of arguments.

Module Module1

    Sub Main()
        ' In the following function call, CalcSum's local variables
        ' are assigned the following values: args(0) = 4, args(1) = 3,
        ' and so on. The displayed sum is 10.
        Dim returnedValue As Double = CalcSum(4, 3, 2, 1)
        Console.WriteLine("Sum: " & returnedValue)
        ' Parameter args accepts zero or more arguments. The sum
        ' displayed by the following statements is 0.
        returnedValue = CalcSum()
        Console.WriteLine("Sum: " & returnedValue)
    End Sub

    Public Function CalcSum(ByVal ParamArray args() As Double) As Double
        CalcSum = 0
        If args.Length <= 0 Then Exit Function
        For i As Integer = 0 To UBound(args, 1)
            CalcSum += args(i)
        Next i
    End Function

End Module

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

See also