參數陣列 (Visual Basic)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.

宣告 ParamArrayDeclaring a ParamArray

您可以使用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. 在程序定義中明確包含ByVal關鍵字是良好的程式設計作法。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.

呼叫 ParamArrayCalling a ParamArray

當您呼叫定義參數陣列的程式時,您可以使用下列任何一種方式提供引數:When you call a procedure that defines a parameter array, you can supply the argument in any one of the following ways:

  • 沒有,也就是說,您可以省略ParamArray引數。Nothing — that is, you can omit the ParamArray argument. 在此情況下,會將空的陣列傳遞給程式。In this case, an empty array is passed to the procedure. 如果您明確地傳遞not 關鍵字,則會將 null 陣列傳遞給程式,如果被呼叫的程式未檢查這個條件,可能會產生 NullReferenceException。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. 每個引數的資料類型必須可以隱含地轉換成 ParamArray 的元素類型。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.

在所有情況下,程式內的程式碼會將參數陣列視為一維陣列,其中包含與 ParamArray 資料類型相同之資料類型的元素。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.

重要

當您處理可能會無限大的陣列時,會有 overrunning 應用程式的一些內部容量的風險。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

下列範例會定義和呼叫函式 calcSumThe following example defines and calls the function calcSum. 參數的 ParamArray 修飾詞 args 可讓函式接受可變數目的引數。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