Compartilhar via


Lista de parâmetros (Visual Basic)

Specifies the parameters a procedure expects when it is called. Multiple parameters are separated by commas. The following is the syntax for one parameter.

[ <attributelist> ] [ Optional ] [{ ByVal | ByRef }] [ ParamArray ] 
parametername[( )] [ As parametertype ] [ = defaultvalue ]

Parts

  • attributelist
    Optional. List of attributes that apply to this parameter. You must enclose the Lista de atributos (Visual Basic) in angle brackets ("<" and ">").

  • Optional
    Optional. Specifies that this parameter is not required when the procedure is called.

  • ByVal
    Optional. Specifies that the procedure cannot replace or reassign the variable element underlying the corresponding argument in the calling code.

  • ByRef
    Optional. Specifies that the procedure can modify the underlying variable element in the calling code the same way the calling code itself can.

  • ParamArray
    Optional. Specifies that the last parameter in the parameter list is an optional array of elements of the specified data type. This lets the calling code pass an arbitrary number of arguments to the procedure.

  • parametername
    Required. Name of the local variable representing the parameter.

  • parametertype
    Required if Option Strict is On. Data type of the local variable representing the parameter.

  • defaultvalue
    Required for Optional parameters. Any constant or constant expression that evaluates to the data type of the parameter. If the type is Object, or a class, interface, array, or structure, the default value can only be Nothing.

Comentários

Parameters are surrounded by parentheses and separated by commas. A parameter can be declared with any data type. If you do not specify parametertype, it defaults to Object.

When the calling code calls the procedure, it passes an argument to each required parameter. For more information, see Diferenças entre parâmetros e argumentos (Visual Basic).

The argument the calling code passes to each parameter is a pointer to an underlying element in the calling code. If this element is nonvariable (a constant, literal, enumeration, or expression), it is impossible for any code to change it. If it is a variable element (a declared variable, field, property, array element, or structure element), the calling code can change it. For more information, see Diferenças entre argumentos modificáveis e não modificáveis (Visual Basic).

If a variable element is passed ByRef, the procedure can change it as well. For more information, see Diferenças entre passar um argumento por valor e por referência (Visual Basic).

Rules

  • Entre parênteses. If you specify a parameter list, you must enclose it in parentheses. If there are no parameters, you can still use parentheses enclosing an empty list. This improves the readability of your code by clarifying that the element is a procedure.

  • Opcional Parâmetros. Se você usar o Optional modificador em um parâmetro, todos os parâmetros subseqüentes na lista também pode ser opcionais e ser declarados usando a Optional modificador.

    Every optional parameter declaration must supply the defaultvalue clause.

    For more information, see Parâmetros opcionais (Visual Basic).

  • Parameter Arrays. Você deve especificar ByVal para um ParamArray parâmetro.

    You cannot use both Optional and ParamArray in the same parameter list.

    For more information, see Matrizes de parâmetros (Visual Basic).

  • Passing Mechanism. O mecanismo padrão para cada argumento é ByVal, o que significa que o procedimento não é possível alterar o elemento variável subjacente. However, if the element is a reference type, the procedure can modify the contents or members of the underlying object, even though it cannot replace or reassign the object itself.

  • Nomes de parâmetro. Se o parâmetrodo tipo de dados é uma matriz, execute parametername imediatamente por parênteses. For more information on parameter names, see Nomes de elementos declarados (Visual Basic).

Exemplo

The following example shows a Function procedure that defines two parameters.

Public Function howMany(ByVal ch As Char, ByVal st As String) As Integer
End Function
Dim howManyA As Integer = howMany("a"c, "How many a's in this string?")

Consulte também

Tarefas

Como: Quebrar e combinar instruções no código (Visual Basic)

Referência

Instrução Function (Visual Basic)

Instrução Sub (Visual Basic)

Instrução Declare

Instrução Structure

Opção declaração estrito

Atributos (C# e Visual Basic)

DllImportAttribute