I have a custom class ( a Vector class). I would like to override the “=” operator to return True if the comparison Vector is the same object or if it’s x and y properties are equal.

I know this works for comparing the x and y values of the Vector class (taken from the LR):

[code]Function Operator_Compare(rightSide As Vector) As Integer
Dim a, b As Integer
a = Self.x ^ 2 + Self.y ^ 2
b = rightSide.x ^ 2 + rightSide.y ^ 2

If a > b Then Return 1
If a = b Then Return 0
If a < b Then Return -1
End Function[/code]

but what I’m unsure about is how to return True if rightSide is the same object. I know this is not super relevant to vectors which only have two properties but it would be useful to know for more complex classes which may have multiple properties.

On a related note, is it possible to overload via a static method rather than on an instance?

Take the vector class in the docs. There is an example of how to overload the ‘+’ operator to add a vector to an instance of a vector but this would add the rightSide vector to self. What if I want to do this:

vector3 = vector1 + vector2

or does Xojo require a static method to do this, like below?

There are two operators you need to overload for +:

Function Operator_Add (rhs as Vector) as Vector
Function Operator_AddRight (lhs as Vector) as Vector
Same for the other operators:

Operator_And and Operator_AndRight
Operator_Divide and Operator_DivideRight
Operator_IntegerDivide and Operator_IntegerDivideRight
Operator_Modulo and Operator_ModuloRight
Operator_Multiply and Operator_MultiplyRight
Operator_Or and Operator_OrRight
Operator_Power and Operator_PowerRight
Operator_Subtract and Operator_SubtractRight
Operator_XOr and Operator_XOrRight

[quote=248102:@Garry Pettet]Take the vector class in the docs. There is an example of how to overload the ‘+’ operator to add a vector to an instance of a vector but this would add the rightSide vector to self. What if I want to do this:
vector3 = vector1 + vector2[/quote]
I just re-read your question. The example in the docs do exactly what you want: they return a new vector the rhs vector is not added to Self.

So this depends entirely of your code in the Operator_Add function.

Function Operator_Add (rhs as Vector) as Vector
Dim ret As New Vector()
ret.x = Self.x + rhs.x
ret.y = Self.y + rhs.y
Return ret
End
> vector3 = vector1 + vector2

Operator_Add (rhs as Vector) as Vector
Self.x = Self.x + rhs.x
Self.y = Self.y + rhs.y
Return Self
End
> vector1 = vector1 + vector2