Question

I am trying to implement System.Collections.IComparer in Progress 4gl.

With the following code, I get the error: Incompatible data types in expression or assignement. (223)

CLASS Tools.Comparer.ItemByItemNo IMPLEMENTS System.Collections.IComparer: 

    METHOD PUBLIC INTEGER Compare(
        INPUT o1 AS System.Object
        ,INPUT o2 AS System.Object):

        /* declaration */
        DEFINE VARIABLE oItem1 AS Entity.Item NO-UNDO.
        DEFINE VARIABLE oItem2 AS Entity.Item NO-UNDO.

        /* cast */
        ASSIGN oItem1 = CAST(o1, "Entity.Item").
        ASSIGN oItem2 = CAST(o2, "Entity.Item").

        /* compare */
        RETURN Tools.String:Compare(oItem1:ItemNo, oItem2:ItemNo).

    END METHOD.

END CLASS.

Is it possible to cast a class from System.Object?

Thank you! Sebastien

/* Here the code of compare to mimic the .NET compare in progress */

METHOD STATIC PUBLIC INTEGER Compare(
    INPUT String1 AS CHARACTER
    ,INPUT String2 AS CHARACTER):

    IF string1 < string2 THEN
        RETURN -1.
    ELSE IF string1 = string2 THEN
        RETURN 0.
    ELSE IF string1 > string2 THEN
        RETURN 1.

END METHOD.
Was it helpful?

Solution

From the Knowledge Base:

Cause

This is expected behavior. The code is trying to assign an ABL class into a property whose type is System.Object. This cannot be done. The compiler error is correct (incompatible types). All .NET classes inherit from System.Object, which in turn inherits from Progress.Lang.Object, but ABL classes do not inherit from System.Object, i.e. the hierarchy is:

P.L.O
   |
<user-defined class>

Workaround

In order to be able to assign an ABL class into a System.Object, it must inherit from System.Object:

P.L.O
   |
 S.O
   |
<.NET classes>

Sample Code

USING Progress.Lang.*.
USING System.*.

CLASS a INHERITS Object:

    ...

END CLASS.

See this knowledgebase entry for a complete description

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top