# Comparing two classes

I am porting some C# code to Xojo.

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.

```If Self Is rightSide Then Return 0 ... code to set a and b If a > b Then Return 1 If a = b Then Return 0 If a < b Then Return -1```

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?

``vector3 = Vector.Add(vector1, vector2)``

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```

What’s the difference? The docs don’t really explain…

In one case Self is the lhs, in the other Self is the rhs. Implement both.

Thanks Eli

[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.

```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

```Dim v1 As New Vector(10, 20) Dim v2 As Vector = 3 + v1 // add 3 to bot x and y, note that 3 is the lhs and v1 is the rhs```
```Function Operator_AddRight(lhs as Int32) As Vector Dim ret As New Vector() ret.x = lhs + Self.x ret.y = lhs + Self.y Return ret End Function```