Export (0) Print
Expand All

Parameter Arrays

Updated: October 2008

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.

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.

For more information, see How to: Define a Procedure with an Indefinite Number of Parameters.

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. You can also pass the Nothing (Visual Basic) keyword, with the same effect.

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

For more information, see How to: Call a Procedure that Takes an Indefinite Number of Parameters.

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, 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 How to: Determine the Size of an Array.

Description

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

Code

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

Date

History

Reason

October 2008

Added an example.

Customer feedback.

Community Additions

ADD
Show:
© 2014 Microsoft