ByRef Keyword

Used to pass a parameter by reference.

# Notes

ByRef parameter

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

By default, you pass parameters to methods by value. When you do so, the method receives a copy of the data in the object that you pass. Your method receives the data and can perform operations on it.

To pass a parameter by reference, you use the ByRef keyword in the parameter declaration area within the Add Method pane. 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 object 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, you can't do this because the parameter only represents a copy of the data itself.

If the parameter is an array, ByRef works in the same way as if the parameter were a variable. Your method can replace the array or only some elements in the array.

# Example

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 powers(ByRef a As Integer)

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

The method is simply:

a=a*a

Powers is called in the following code:

Dim a as Integer
a=3
powers a
EditField1.text= Str(a)

The EditField displays the value of 9.

# See Also

ByVal keyword.