Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Instantiating and managing class-based objects : Defining an object reference field in a temp-table
 

Defining an object reference field in a temp-table

You can define temp-table fields as the class type, Progress.Lang.Object, the built-in class that is the implicit super class (root class) of all other classes in ABL. This allows a temp-table field to hold object references of any class type. When object references are assigned to the field, they are implicitly cast to the root class, Progress.Lang.Object. When you want to use the object reference as a specific object type, you must cast the object reference to the specific type. For more information on casting, see Object reference assignment and casting.
This is the syntax to define a class field in a temp-table:

Syntax

FIELD field-name AS [ CLASS ] Progress.Lang.Object
Element description for this syntax diagram follow:
field-name
The name of a field defined as the ABL root class.
Note: You cannot define a class field in an OpenEdge database table.
The following example defines a temp-table field to hold a class instance, by defining the field as Progress.Lang.Object. In this case, the purpose is to store different ShapeClass instances in order to calculate an area on each ShapeClass object in sequence, according to its subclass type (RectangleClass or CircleClass), as shown:
USING Progress.Lang.*.

CLASS Main:
  DEFINE VARIABLE length AS DECIMAL NO-UNDO INITIAL 5.0.
  DEFINE VARIABLE radius AS DECIMAL NO-UNDO INITIAL 100.0.
  DEFINE VARIABLE width  AS DECIMAL NO-UNDO INITIAL 10.0.

  DEFINE TEMP-TABLE myTT NO-UNDO
    FIELD Shape AS CLASS Object
    FIELD Area  AS DECIMAL.

  CONSTRUCTOR PUBLIC Main ( ):
    CREATE myTT.
    myTT.Shape = NEW RectangleClass (width, length).

    CREATE myTT.
    myTT.Shape = NEW CircleClass (radius).

    FOR EACH myTT:
      /* Cast the field to the common shape super class, ShapeClass */
displayArea(INPUTCAST(myTT.Shape, ShapeClass), OUTPUT myTT.Area).
    END.
  END CONSTRUCTOR.

  METHOD PUBLIC VOID displayArea
    (INPUT rShape AS CLASS ShapeClass, OUTPUT dArea AS DECIMAL):
    dArea = rShape:calculateArea( ).
    MESSAGE dArea VIEW-AS ALERT-BOX.
  END METHOD.
END CLASS.
This previous example is a modified version of the Main class used to demonstrate polymorphism (see Using polymorphism with classes). This version of the Main class shows how you might use temp-table fields to support polymorphic object references and use the CAST function to cast down to the common functionality of the polymorphic super class (ShapeClass).