Comparing objects (less than / greater than)

Posted by Lieven De Foor on 29-Mar-2017 06:05

Hi,

Currently you can write the following code:

CLASS MyClass:

    DEFINE PUBLIC PROPERTY Value AS INTEGER NO-UNDO
    GET.
    SET.

END CLASS.

/****************************************************/

DEFINE VARIABLE MyFirstObject AS MyClass NO-UNDO.
DEFINE VARIABLE MySecondObject AS MyClass NO-UNDO.

MyFirstObject = NEW MyClass().
MySecondObject = NEW MyClass().

MyFirstObject:Value = 2.
MySecondObject:Value = 1.

MESSAGE MyFirstObject < MySecondObject SKIP
        MyFirstObject:Value < MySecondObject:Value
    VIEW-AS ALERT-BOX.

While this code compiles, it is useless as you can't influence how the < or > operators are interpreted.

The first created object seems to be always less than the second created one, which leads me to believe that the internal handle value gets compared.

There should be a way to influence this, e.g. by implementing a IComparable interface like in c#

https://msdn.microsoft.com/en-us/library/system.icomparable(v=vs.110).aspx

Or am I missing something and is this possible in ABL?

All Replies

Posted by Lieven De Foor on 29-Mar-2017 06:14

Besides implementing the IComparable interface by creating a CompareTo() method, this would still need some form of operator overloading to make this actually work using the < or > operators...

Posted by Lieven De Foor on 29-Mar-2017 06:28
Posted by Peter Judge on 29-Mar-2017 07:48

FYI, operator overloading does exist in one case – for Enum types.
 
Otherwise you are correct.

Posted by Tim Kuehn on 29-Mar-2017 12:39

Using a class variable reference w/out citing a method defaults to the "tostring" method.

Override that method and you can get this to work.

Posted by Peter Judge on 29-Mar-2017 12:44

The MESSAGE statement is sometimes a little bit special in how it treats / converts things.

Try replacing

    MyFirstObject < MySecondObject

with

    INT64(MyFirstObject) < INT64(MySecondObject)

or even messaging those INT64() values and seeing what you get.

Again, ENUM types do NOT follow this rule ... the INT64() function will return the enumeration's value (not an "object reference id").

Posted by Lieven De Foor on 30-Mar-2017 02:43

We have objects that mimic Enums (before the Enum functionality was introduced in the language) and with more properties than an Enum (Name, Value, ShortLabel, LongLabel, UntranslatedShortLabel, UntranslatedLongLabel).

We have implemented ToString() to return the Name of the object, whereas we would logically sort on the Value, so changing ToString isn't feasible...

This thread is closed