Yeah
Don’t use byref like you are - its unnecessary
Sub myFunction( v() As Integer)
' v is a reference to an array of ints
End Sub
Arrays are already passed by reference anyways
So what I gave you will allow you to manipulate the members of the array but not change the array reference itself
Yours would actually let you do
Sub myFunction( ByRef v() As Integer)
dim someNewArray(-1) as Integer
v = someNewArray
End Sub
and change out the array for a new one
When you pass an object or array to a function, you are already passing a reference, not a copy. (The same is true with strings). There is no performance advantage in using ByRef.
Now, if you want the method to be able to change the value of the original variable, that’s when you’d use ByRef. For example:
dim v() as Integer = Array( 1, 2, 3 )
SomeMethod( v )
// Without ByRef
Sub SomeMethod (arr() As Integer)
// arr has a reference to the same array that v points to
arr = Array( 4, 5, 6 )
// arr has a reference to a brand-new array
End Sub
// v still equals 1, 2, 3
// With ByRef
Sub SomeMethod (ByRef arr() As Integer)
// arr is a stand-in for the variable v
arr = Array( 4, 5, 6 )
End Sub
// Now v contains a new array 4, 5, 6
Correct, because arr points to the same array that v points to, and I modified the array. It’s the same as this:
dim a() as Integer = Array( 1, 2, 3 )
dim b() as Integer = a // Both hold the same array
a.Append 9
b.Append 0
// Since there is only one array, both a and b point to the values (1, 2, 3, 9, 0)
In the case of an reference type (which strings & arrays are) what you pass is a reference.
And if you pass that byref now the reference is byref meaning you can change what it refers to.
Essentially reference types are implicitly “const” unless you explicitly say “byref” which most times you really don’t want to.
In reality most code in Xojo doesn’t use “byref” much
Leave it out until you absolutely HAVE to have it and you’ll be correct more often than not