Objects as Procedure Arguments

Like other variables and values, object values may be passed as procedure arguments.  Object values are always passed as pointers, so the operation of ByVal and ByRef is a little different from that of other arguments.

ByVal 

When an object value is passed ByVal, a pointer to the object value is passed to the called procedure.  That means that changes made to the value via the called procedure parameter are seen by the caller.  But changes made to the variable are not seen by the caller.

For example:

Sub My_Sub (ByVal Loc As Location)
Loc.X = 20 ' Changes original value
Loc = New Location ' Create new value locally
Loc.X = 30 ' Changes local value
End Sub

Sub Main()
Dim Loc1 As New Location ' Create new location value
Dim Loc2 As Location ' Does not create value
Dim tmp As Double
Loc2 = Loc1 ' Copy value pointer
My_Sub ( Loc1 ) ' Pass pointer to Loc1 value
tmp = Loc1.X ' Gets 20 from original value
tmp = Loc2.X ' Gets 20 from original value
End Sub

ByRef  

When an object value is passed ByRef, a pointer to the object variable is passed to the called procedure.  That means that changes made to either the value or the variable via the called procedure parameter are seen by the caller.

For example:

Sub My_Sub (ByRef Loc As Location)
Loc.X = 20 ' Changes original value
Loc = New Location ' Caller variable changed
Loc.X = 30 ' Changes new value
End Sub

Sub Main()
Dim Loc1 As New Location ' Create new location value
Dim Loc2 As Location ' Does not create value
Dim tmp As Double
Loc2 = Loc1 ' Copy value pointer
My_Sub ( Loc1 ) ' Pass pointer to Loc1 variable
tmp = Loc1.X ' Gets 30 from new value
tmp = Loc2.X ' Gets 20 from original value
End Sub