That section of the documentation is just plain wrong. Nothing is copied. There are not 2 separate arrays any more.
var names(2), copyNames(2) as String
// there 2 separate arrays
copyNames = names
// there is now only one array, the original copyNames array has been discarded
// and now both variables refer to the same array
Thanks @Tim_Hare for supporting my claim. We should probably file an Issue against that incorrect documentation.
To @C_L_Hinkle - you are likely confused about Arrays because they are somewhat confusing, and some of the documentation is just wrong.
But the real answer: define a custom Class that does what you really want to do.
An 8x8 gameboard suggests checkers or chess, but if you want to post a new thread about your design, I’m sure a few of us would be happy to help design a new class to do what you want
Because I’m hearing that we pass by value, which means changes in the subroutine wouldn’t affect the array in the calling function. Then someone inserts that arrays are objects so they act differently and changes made in the subroutine do affect the calling parameters. Then that turns into talk about pointers and pointers to pointers which is more conceptual than I’m looking for. Then I see the reiteration that without byRef, the source parameters don’t change. And someone adds that arrays are objects so they do change. And if there’s a definitive agreement on what happens to arrays that are passed to a subroutine, I missed it.
@C_L_Hinkle
Sorry I did not mean you
I was trying to find out what in the documentation was incorrect
Only way to get it fixed is by submitting an issue
var names(2), copyNames(6) As String
names = Array("Fred", "Ginger", "Stanley")
copyNames = names
Below is what seems to be the equivalent statement, and its awkwardness seems yet more evident.
The last statement assigns the values of all three elements of names to the first three elements of copyNames. If copyNames had more elements than names, then the additional elements would first be removed from copyNames and the assignment of all the elements of names to copyNames would be completed.
Yes. But in the case of objects and arrays, the value being passed is the reference to the array, not the array itself. Some people feel more comfortable with the term pointer to the array. That is why any changes made to the array inside the subroutine will also be seen by the calling code.
What I’m trying to draw is the distinction as talking about Xojo and what terms keep getting tossed around:
Not sure I can say but someone very more familiar with Xojo states:
Arrays are NOT passed by REFERENCE
They ARE a reference type which is entirely different
To illustrate
To show its pass by value
Sub FOO( anarray() as integer )
Var anotherarray() as integer = Array(1,2,3)
anarray = anotherarray
system.debuglog anarray.count.ToString
end sub
// and call it
Var original() as integer = array(8)
system.debuglog original.count.ToString
foo(original)
system.debuglog original.count.ToString
Now for the BYREF version where we pass a REFERENCE TYPE BYREF
Sub FOO( byref anarray() as integer )
dim anotherarray() as integer = Array(1,2,3)
anarray = anotherarray
system.debuglog anarray.count.ToString
end sub
I appreciate what you’re trying to say here, but this is a Xojo forum. It’s not helpful to use terms from other languages to describe how Xojo operates, especially when those terms are so easily confused with the words that we are accustomed to within this language and community.
In Xojo, all Objects (including Arrays) are passed by Reference.
Here’s the confusion: The reference parameter itself, however, is by default passed by value (ByVal).
There are 2 levels of indirection, and you have to talk about both for this to make sense.
I hate to mention it given the confusion here, but Strings are also special - they act as if they are passed by value, but they are actually passed by reference, but happen to be immutable, so that any modification to a string creates a new string, leaving the original untouched.
I have no idea what you mean by “reference type”. I’m assuming you are borrowing that term from some other language. There is no such notion in Xojo - at least, nothing that is called “reference type”.
Appreciate that, and I don’t mean to come down too hard on helpful people. Your example literally does a decent job of showing that arrays are in fact passed by reference, in direct conflict with what you state before the example code. Drop the “reference type” terminology; if a Xojo engineer told you that, it’s likely an internal implementation detail of the compiler that doesn’t have any relevance at the code level.
@Tim_Hare
Maybe a bad choice of words on my part as a Pointer is a Pointer is a Pointer
What I was trying to help explain coming from a former Xojo engineer is further down in post