2

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.
Tom Sawyer
  • 835
  • 2
  • 10
  • 32
  • `ASSIGN oItem1 = o1. /* Incompatible data types in expression or assignement. (223) */` – Tom Sawyer Feb 28 '14 at 19:57
  • `ASSIGN oItem1 = (entity.Item) o1. /* Unknown field or variable - Item (201) */` – Tom Sawyer Feb 28 '14 at 19:58
  • `ASSIGN oItem1 = CAST(o1, "Entity.Item"). /* Incompatible data types in expression or assignement. (223) */` – Tom Sawyer Feb 28 '14 at 19:59
  • `ASSIGN oItem1 = CAST(o1, Entity.Item). /* Incompatible data types in expression or assignement. (223) */` – Tom Sawyer Feb 28 '14 at 19:59
  • Have you checked this: http://knowledgebase.progress.com/articles/Article/P132544?q=Cast+from+system.object&l=en_US&fs=Search&pn=1 – Jensd Feb 28 '14 at 21:00
  • 1. What is Entity.Item? Is it a .Net type or an ABL type? 2. What is Tools.String:Compare? 3. This has nothing to do with implementing IComparer – Fabian Frank Feb 28 '14 at 21:43
  • Entity.Item and Tools.String:Compare are custom code. I copied pasted the function code to give a complete example. – Tom Sawyer Mar 01 '14 at 15:10
  • Thank you Jensd, I followed the KB that hints to inherits the class Entity.Item from System.Object. This worked. If you want to post the link as a answer and I will green check your answer. Thank you again! – Tom Sawyer Mar 01 '14 at 15:12

1 Answers1

3

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

Jensd
  • 7,886
  • 2
  • 28
  • 37