ByRef

From Xojo Documentation

Language Keyword

Used to pass a parameter by reference.

Usage

ByRef parameter

Part Type Description
parameter Any data type Parameter to be passed by reference. Parameter can be an array.

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.

fa-info-circle-32.png
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:

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

The Powers method takes one parameter, a, that is called ByRef.

The method code is:

a = a * a

Powers is called in the following code:

Var a As Double
a = 3
Powers(a)
// a = 9

The TextField displays the value of 9.

See Also

ByVal keyword.