Keyword

ByRef


Description

Used to pass a parameter by reference.

Usage

ByRef parameter As DataType

Part

Type

Description

parameter

Parameter to be passed by reference. Parameter can be an array.

DataType

Any valid data type

The data type of the parameter.

Important

Passing parameters with ByRef is not currently supported for Android.

Notes

Parameters passed by value are treated as local variables inside the method-just like variables that are created using the Var statement. This means that you can modify the values of the parameters themselves rather than first assigning the parameter to a local variable. For example, if you pass a value in the parameter "x", you can increment or decrement the value of x rather then assigning the value of x to a local variable that is created using Var.

To pass a parameter by reference, you use the ByRef keyword in the parameter declaration for the method. If you precede a parameter name by the keyword ByRef, you pass information by reference. When you pass information by reference, you actually pass a pointer to the variable containing the information. The practical advantage of this technique is that the method can change the values of each parameter. When you pass parameters by value, it doesn't do this because in effect the parameter only represents a copy of the data itself.

Note

By default all arrays and objects are reference types that are passed ByVal. Because they are reference types, changes made to that array or object will be reflected in the calling method. This is no different than assigning an array or object to a second variable or property.

Sample code

The following method declaration uses ByRef:

ByRef amount As Double

When you click OK to save the new method, the Sub statement in the Code Editor shows that the parameter has been declared ByRef:

Protected Sub AddTax(ByRef amount As Double)

The AddTax method takes one parameter, amount, that is called ByRef.

The method code is:

amount = amount * 1.08

AddTax is called in the following code:

Var a As Double
a = 3
AddTax(a)
TextField1.Text = a.ToString

Compatibility

All project types on all supported operating systems.

See also

ByVal keyword.