Export (0) Print
Expand All

Argument Passing ByVal and ByRef

In Visual Basic, you can pass an argument to a procedure by value or by reference by specifying the ByVal or ByRef keywords, respectively. Passing an argument by value means the procedure cannot modify the contents of the variable element in the calling code underlying the argument. Passing by reference allows the procedure to modify the contents in the same way that the calling code itself can.

Passing arguments by value and by reference is not the same distinction as the classification of data types into value types and reference types. However, the two categories do interact.

Variable and Nonvariable Arguments

The programming element underlying an argument can be either a variable element, capable of having its value changed, or a nonvariable element. The following table lists variable and nonvariable elements.

Variable elements Nonvariable elements
Declared variables, including object variables Constants
Fields (of classes) Literals
Array elements Enumerations
Structure elements Expressions

Nonvariable arguments are never modified in the calling code, even if they are passed by reference. The called procedure might modify its copy of such an argument, but that modification does not affect the underlying element in the calling code.

In This Section

Argument Passing ByVal
Describes passing arguments by value, which means the procedure cannot modify the variable itself.
Argument Passing ByRef
Describes passing arguments by reference, which means the procedure can modify the variable itself.
Argument Passing Mechanism
Summarizes the interaction between the element's data type and the passing mechanism.

Related Sections

Procedures
Provides links to topics about Visual Basic procedures, including information on the different procedure types and how they are called.
Show:
© 2014 Microsoft