Compartilhar via


Como: Alterar o valor de um argumento de procedimento (Visual Basic)

When you call a procedure, each argument you supply corresponds to one of the parameters defined in the procedure. In some cases, the procedure code can change the value underlying an argument in the calling code. In other cases, the procedure can change only its local copy of an argument.

When you call the procedure, Visual Basic makes a local copy of every argument that is passed ByVal (Visual Basic). For each argument passed ByRef (Visual Basic), Visual Basic gives the procedure code a direct reference to the programming element underlying the argument in the calling code.

Se o elemento base no código de chamada é um elemento pode ser modificado e o argumento é passado ByRef, o código do procedimento pode usar a referência direta para alterar o valor do elemento no código de chamada.

Changing the Underlying Value

To change the underlying value of a procedure argument in the calling code

  1. In the procedure declaration, specify ByRef (Visual Basic) for the parameter corresponding to the argument.

  2. In the calling code, pass a modifiable programming element as the argument.

  3. In the calling code, do not enclose the argument in parentheses in the argument list.

  4. In the procedure code, use the parameter name to assign a value to the underlying element in the calling code.

See the example further down for a demonstration.

Changing Local Copies

Se o elemento base no código de chamada é um elemento nonmodifiable, ou se o argumento for passado ByVal, o procedimento não é possível alterar seu valor no código de chamada. However, the procedure can change its local copy of such an argument.

To change the copy of a procedure argument in the procedure code

  1. In the procedure declaration, specify ByVal (Visual Basic) for the parameter corresponding to the argument.

    - ou -

    In the calling code, enclose the argument in parentheses in the argument list. This forces Visual Basic to pass the argument by value, even if the corresponding parameter specifies ByRef.

  2. In the procedure code, use the parameter name to assign a value to the local copy of the argument. The underlying value in the calling code is not changed.

Exemplo

The following example shows two procedures that take an array variable and operate on its elements. The increase procedure simply adds one to each element. The replace procedure assigns a new array to the parameter a() and then adds one to each element.

Public Sub increase(ByVal a() As Long)
    For j As Integer = 0 To UBound(a)
        a(j) = a(j) + 1
    Next j
End Sub
Public Sub replace(ByRef a() As Long)
    Dim k() As Long = {100, 200, 300}
    a = k
    For j As Integer = 0 To UBound(a)
        a(j) = a(j) + 1
    Next j
End Sub
Dim n() As Long = {10, 20, 30, 40}
Call increase(n)
MsgBox("After increase(n): " & CStr(n(0)) & ", " & 
    CStr(n(1)) & ", " & CStr(n(2)) & ", " & CStr(n(3)))
Call replace(n)
MsgBox("After replace(n): " & CStr(n(0)) & ", " & 
    CStr(n(1)) & ", " & CStr(n(2)) & ", " & CStr(n(3)))

O primeiro MsgBox chamada exibe "após increase(n): 11, 21, 31, 41". Because the array n is a reference type, replace can change its members, even though the passing mechanism is ByVal.

A segunda MsgBox chamada exibe "depois de substituir(n): 101, 201, 301". Because n is passed ByRef, replace can modify the variable n in the calling code and assign a new array to it. Because n is a reference type, replace can also change its members.

You can prevent the procedure from modifying the variable itself in the calling code. See Como: Proteger um argumento de procedimento contra alterações de valor (Visual Basic).

Compilando o código

When you pass a variable by reference, you must use the ByRef keyword to specify this mechanism.

The default in Visual Basic is to pass arguments by value. However, it is good programming practice to include either the ByVal (Visual Basic) or ByRef (Visual Basic) keyword with every declared parameter. This makes your code easier to read.

Segurança

There is always a potential risk in allowing a procedure to change the value underlying an argument in the calling code. Make sure you expect this value to be changed, and be prepared to check it for validity before using it.

Consulte também

Tarefas

Como: Passar argumentos para um procedimento (Visual Basic)

Como: Proteger um argumento de procedimento contra alterações de valor (Visual Basic)

Como: forçar um argumento a ser passado por valor (Visual Basic)

Conceitos

Procedimentos no Visual Basic

Parâmetros e argumentos de procedimento (Visual Basic)

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

Diferenças entre argumentos modificáveis e não modificáveis (Visual Basic)

Diferenças entre passar um argumento por valor e por referência (Visual Basic)

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

Outros recursos

Implementação de tipos de dados (Visual Basic)