Compartilhar via


Como: definir um procedimento com um número indefinido de parâmetros (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 Matrizes de parâmetros (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 Opcional (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. O código do procedimento , determinar o comprimento da matriz passada para a matriz de parâmetro , chamando o UBounddefunção em nome de matriz de parâmetro .

  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.

Observação de segurançaObservação sobre segurança

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.

Exemplo

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)

Compilando o código

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

Consulte também

Tarefas

Como: chamar um procedimento que recebe um número indefinido de parâmetros (Visual Basic)

Referência

ByVal (Visual Basic)

ParamArray (Visual Basic)

Conceitos

Parâmetros e argumentos de procedimento (Visual Basic)

Passando argumentos por valor e por referência (Visual Basic)

Passagem de argumentos por posição e nome (Visual Basic)

Parâmetros opcionais (Visual Basic)

Sobrecarga de procedimento (Visual Basic)

Verificação de Tipo no Visual Basic

Matrizes no Visual Basic