Operator_compare

Ever wanted to define a class, maybe a vector class, and make it possible to write code like

dim v1 as new Vector(1,1)
dim v2 as new Vector(2,2)

if v1 > v2 then
  // do something when v1 > v2
elseif v1 < v2 then
  // do something when v1 < v2
else
  // do something when v1 = v2
end if

Perhaps you have some other kind of class you’d like to define that has some kind of custom mechanism to compare itself to other instances – or even other data types.

If so you need to know how to implement operator_compare

First off operator_compare can ONLY be implemented in a Class. You cannot extend existing classes and add it to them.

It’s a method, or several methods, you add to a class that will be called whenever you use the comparison operators =, <, >, <=, >= and <> are used. Note that this has implications for your code if you are using = to check is one instance is the same instance as another. In that case you should probably use IS rather than =.

Beyond that operator_compare is pretty straight forward.

You define the method with that name, and the parameter is whatever type you wish to compare your instance, the “self” instance, to. So you can have many as each signature would be different. The return value is and integer that is

  • < 0 means SELF is “less than” the passed parameter
  • = 0 means SELF is “equal to” the passed parameter
  • > 0 means SELF is “greater than” the passed parameter

and you get to define what less than, equal to, and greater than mean.

So suppose our Vector class was defined as follows

Class Vector
   protected x as double
   protected y as double

   Public Sub Constructor(x as double, y as double)
      Self.x = x
      Self.y = y
   End Sub

   Function Operator_compare(other as vector) as Integer
      Dim a, b As Integer
      a = Self.x ^ 2 + Self.y ^ 2
      b = other.x ^ 2 + other.y ^ 2
      If a > b Then Return 1
      If a = b Then Return 0
      If a < b Then Return -1
    End Function
End Class

And now our original code from way back would work

dim v1 as new Vector(1,1)
dim v2 as new Vector(2,2)

if v1 > v2 then
  // do something when v1 > v2
elseif v1 < v2 then
  // do something when v1 < v2
else
  // do something when v1 = v2
end if

Now you could make Vectors compare themselves to Strings if you really wanted. I’m not sure what exactly that might mean – but you could. You might try implementing this as

Public Function Operator_compare1(other As string) as Integer
  Dim myRepresentation As String = "(" + Str(x,"-######.00") + "," + Str(y,"-######.00") + ")"
  
  If myRepresentation = other Then Return 0
  
  return -1
End Function

Again I’m not sure what this might truly mean but we’ll indicate that unless they are the exact same string the “self” is < the string value. Suppose you try to test this with

Dim v1 As New Vector(1,1)

If v1 = "(foo)" Then
  Break
Else
  Break
End If

And now you Vectors can be compared to Strings. With extra overloads of Operator_compare you can make your classes compare themselves to all sorts of your other classes & types.

Enjoy !