Structures (If I remember correctly) are essentially memory blocks , and I have a suspicion that they would already be ‘by ref’
Do you want to make the date pointed to by tri.p(0) swap with the data that was held in tri.p(2),
leaving the pointers alone?
Or do you want to make the pointer in tri.p(0) and the pointer in tri.p(2) change places, leaving the data where it was?
If you pass pointers into the Swap, then you are only sending Integers, and they are fine as byRef parameters.
ByRef remains one of the more conceptually difficult concepts in Xojo.
ByRef takes a reference to a variable or property that holds a value, allowing you to manipulate the contents of that variable or property in a method. As such, you must pass a variable or property, not an array element or static value.
If you want a method that swaps array elements, take the array and indexes as mentioned above.
ByRef illustration:
Sub M1( a As Integer )
a = 5
End Sub
Sub M2 ( ByRef a As Integer )
a = 5
End Sub
var x as integer = 2
M1( x ) // x remains 2
M2( x ) // The method has changed the contents of x to 5
So the fundamental issue with passing someArray(index) to a ByRef parameter is that you’re not actually passing something with its own memory address. The array itself (someArray) has a memory address because it is an object in its own right but the elements contained within do not, at least when you refer to them via index. This also applied to class properties (someObject.someProperty).
Essentially, anything you want to pass to a ByRef parameter must be declared by Dim or Var in the scope local to the call containing the ByRef.
That is not how references to objects Work. Objects are pased as Value because the “value” that is pased is just a POINTER to the object, not the object itself or a copy of it.
Actually YES, it is if you are using ByRef. You are “calculating” the pointer to the pointer of the object/Array
Is it worth mentioning that if the structure is turned into a class (even if the class simply wraps the structure), then the class can be given a .clone method.
Var tempz as new XYZ
tempz.clone (tri.p(0))
tri.p(0).clone (tri.p(1))
tri.p(1).clone (tempz)
sub Clone( fromitem as XYZ)
x = fromitem.x
y = fromitem.y
//etc
end sub
Oh my… I seem to have started a thread!!!
This is what happens when you code at 2AM.
Looking at the code, it does seem like a bad idea to pass an array element by ref or not.
Although judging by some of the comments it seems like maybe a better approach would have been to pass the a triangle to a method called swap and let it change the Points in the array.
I seem to remember in C there was a SWAP that was inline and typeless.
I am considering converting the structure to a class adding a method to it.