Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Using built-in system and object reference elements : THIS-OBJECT system reference
 

THIS-OBJECT system reference

THIS-OBJECT is a system reference available for use within an instantiated class. At run time, it returns the currently running instance of the class as an object reference. It supports several features:
*Allows a method of the class to pass a reference to the currently instantiated class hierarchy as a parameter or to return a reference to itself as a method return value.
*Allows you to call an instance method from within the class hierarchy where the method is defined when the method name is a reserved ABL keyword. To make such a method call, you use THIS-OBJECT as an object reference to invoke the method. (This is the only situation where you can and must use the THIS-OBJECT system reference to call a method.)
*Allows you to access a variable data member or a class property from within the class hierarchy where the variable data member or class property is defined when the name is a reserved ABL keyword.
*In general, you can use THIS-OBJECT as an object reference to call any instance method defined and available in the current hierarchy of a class as an aid to readability. For example, you might, as a convention, omit THIS-OBJECT for calling methods implemented by the current class definition, and use it as a documentary object reference for calling methods inherited by the current class definition.
Note: You cannot use the THIS-OBJECT system reference to access a static method.
The following example shows a class, acme.myObjs.NewCustomer, that inherits from the sample class acme.myObjs.CustObj:
USING acme.myObjs.*.
USING acme.myObjs.Common.*.

CLASS acme.myObjs.NewCustomer INHERITS CustObj:
  DEFINE PRIVATE VARIABLE rHelperClass AS CLASS HelperClass NO-UNDO.

  CONSTRUCTOR PUBLIC NewCustomer( ):
    /* Create an instance of the HelperClass class */
    rHelperClass = NEW HelperClass( ).
  END CONSTRUCTOR.

  METHOD PUBLIC VOID setNewCustObj( ):
    rHelperClass:InitializeDate(THIS-OBJECT).   END METHOD.
  ...
END CLASS.
This is not one of the installed sample classes, but is created here to illustrate a use of the THIS-OBJECT system reference. A call to its setNewCustObj( ) method passes an object reference to the current instance of itself (THIS-OBJECT) to the InitializeDate( ) method of the sample class, acme.myObjs.Common.HelperClass, which timestamps itself, as shown here:
USING acme.myObjs.*.
USING acme.myObjs.Common.*.
USING acme.myObjs.Interfaces.*.

CLASS acme.myObjs.Common.HelperClass:
  ...
  DEFINE PRIVATE VARIABLE rMsg AS CLASS MsgObj NO-UNDO.
  ...

  METHOD PUBLIC VOID InitializeDate (INPUT prObject AS CLASS CommonObj):
    /* Timestamp this object */
    IF VALID-OBJECT(prObject) THEN
      prObject:updateTimestamp ( ).
    ELSE
      rMsg:Alert("Not a valid object").
  END METHOD.
  ...
END CLASS.
Because the THIS-OBJECT input parameter also represents a subclass of the sample class, acme.myObjs.Common.CommonObj, the InitializeDate( ) method can invoke the updateTimestamp( ) method on the object reference parameter (prObject) to timestamp the object, which setNewCustObj( ) can reference when InitializeDate( ) returns in the acme.myObjs.NewCustomer class shown previously.
In the following example, a method of the Reserved class, DisplayToday( ), calls the Now( ) method, also defined in the class:
CLASS Reserved:
  METHOD PUBLIC DATETIME-TZ Now( ):
    RETURN NOW.
  END METHOD.

  METHOD PUBLIC VOID DisplayToday( ):
    MESSAGE "Current date and time: " THIS-OBJECT:Now( ) VIEW-AS ALERT-BOX.
  END METHOD.
END CLASS.
The name of the method is an ABL reserved keyword, which is the name of the NOW function that also happens to be invoked by the method in this example. If THIS-OBJECT is not used to prefix the Now( ) function call, ABL returns a compiler error indicating that it cannot recognize the method.