This documentation is archived and is not being maintained.

Argument Passing ByRef

Visual Studio .NET 2003

If you pass a variable argument by reference using the ByRef keyword, the procedure can modify the variable itself. In particular, if the argument is an object variable, you can assign a new object to it. This reassignment also affects the object variable in the calling program.

In the example provided in Argument Passing ByVal, if the Replace subroutine had instead taken the argument array by reference, the new array K() would have been assigned to the array variable N in the calling code. The results for N() would then have been as follows:

MsgBox("After Replace(N) taking A() argument ByRef: " & _
              CStr(N(0)) & CStr(N(1)) & CStr(N(2)) & CStr(N(3)))
' The element values of N() are now 101, 201, and 301. Because N was 
' passed ByRef, Replace could modify the variable N and assign a new 
' array to it. Because N is a reference type, Replace could also change 
' its members.

See Also

Argument Passing Mechanism | Argument Passing by Position and by Name | ByVal | ByRef

Show: