How to make a swap method for a structure?

Yes, this is how it should work. Only that the Xojo compiler has always gotten this wrong. Part of the problem is how the syntax parser was originally written. I had, long ago, seen that it had an error in the syntax definition. I had written a corrected version and suggested the fix to Real Software back then, but the engineers didn’t understand and argued that their version was “correct”. Yes, it was consistent but not user-friendly. They didn’t understand the difference, it seemed to me.

it becomes even more silly when you compare these:

class A
  property x as Integer
  sub test(byref v as Integer)
    ...
  end sub
  sub mainTest
    test (x) // works
    test (self.x) // does not work
  end mainTest
end class

Both would generate the SAME code, yet Xojo’s parser rejects the “self.x” version because it can’t scan it as it should.

So, whenever you get this issue, you have to use a local variable as a helper like this:

var tmp as SomeType = anObject.aProp
methodWithByRefParm (tmp)
aProp.aPropOrStruct = tmp

This should work with any type that aPropOrStruct has, even if it’s a structure type.

The compiler should perform this temporary assignment by itself, just like it works in any other modern language, but it doesn’t. Sad but has been this way forever.

1 Like