Try OpenEdge Now
skip to main content
Object-oriented Programming
Programming with Class-based Objects : Instantiating and managing class-based objects : Accessing data members and properties
 

Accessing data members and properties

You can directly access data members and properties from within the class where they are defined by using the data member or property name anywhere you can use a variable. This includes all data members or properties defined directly within the class definition and all PUBLIC and PROTECTED data members or properties defined in any super class of the class hierarchy. For properties, access also depends on whether they are defined as readable or writable, or both. For more information, see Definingproperties within a class.
Note: This section describes data member and property access mechanisms that apply in common to both instance and static data elements and to instance data elements in particular. For information on the unique features of accessing static data members and properties, see Accessingstatic members.
You can access a PUBLIC instance data member or property from outside the class hierarchy of an object where it is defined by using an object reference to qualify the data member or property name. Also, note that only variable data members and properties can be PUBLIC.
While you can also define non-public properties, a primary benefit of properties is to encapsulate non-public data members. Thus, because properties can be defined for many variable data types, PUBLIC properties are well-suited for encapsulating variable data members.
There is no direct access to PRIVATE or PROTECTED data members or properties from outside the class hierarchy, although an instance can access a PRIVATE member of another instance if they are the same class, and an instance can access the PROTECTED member of a second instance that is at the same level or higher in the class hierarchy. The following code demonstrates how an instance can access the PRIVATE data member of another instance of the same class.
/* Defines a class with one private data member and one public method*/
CLASS PrivateNumber:

DEFINE PRIVATE VARIABLE iMyNumber AS INTEGER NO-UNDO.

CONSTRUCTOR PUBLIC PrivateNumber(INPUT piNum AS INTEGER):
iMyNumber = piNum.
END CONSTRUCTOR.

METHOD PUBLIC VOID ShowNumbers(INPUT pOtherInstance AS CLASS PrivateNumber):
MESSAGE "My number is" STRING(iMyNumber) SKIP
"The other instance's number is" STRING(pOtherInstance:iMyNumber)
VIEW-AS ALERT-BOX.
END METHOD.

END CLASS.
/* Creates two instances of PrivateNumber and invokes ShowNumbers*/
DEFINE VARIABLE InstanceOne AS CLASS PrivateNumber NO-UNDO.
DEFINE VARIABLE InstanceTwo AS CLASS PrivateNumber NO-UNDO.

InstanceOne = NEW PrivateNumber(1).
InstanceTwo = NEW PrivateNumber(2).

InstanceOne:ShowNumbers(InstanceTwo).
The constructor of PrivateNumber takes an integer as input, which it then assigns to the PRIVATE data member iMyNumber. The ShowNumbers method takes an object as input and then displays iMyNumber for both the current instance and the instance passed to it. Because instances of the same class can access each other’s private data members, the invocation of ShowNumbers for InstanceOne with InstanceTwo as a parameter will display the private data members of both instances.
If you want to expose a non-variable (such as a buffer, temp-table, query, or ProDataSet) to other classes, you must do so using one of these mechanisms:
*Implement PUBLIC methods that pass the data object as a parameter.
*Define a PUBLIC HANDLE variable, data member, or property that allows access to the data object through its handle. Note that handle data members inherently undermine encapsulation because they provide direct access to the data objects they reference. To encapsulate access to all non-variable data members, including data objects, define non-PUBLIC and static versions of these data members and pass them as parameters (if possible) to PUBLIC methods of the class.
Note: Some data objects, such as query objects, can be defined as static data objects, but can only be passed as parameters using their handles.
* Referencing a public data member or property outside of the object that defines it