Differentiating Between Properties and Methods

A method is a procedure or routine, associated with one or more classes, that acts on an object.

A property is an attribute of an object.

PeopleSoft recommends that you use read/write or read-only properties instead of creating methods named GetXxx and SetXxx. For example, instead of creating a method GetName, that takes no parameter, just to return the name of an object, create a read-only property Name.

If your properties simply set or return the value of an instance variable, it is far more efficient to declare the property that way than have a get and set method. The overhead of having a get and set method means that each time the property is reference some PeopleCode has to be run.

Application classes can have read-only properties, but no indexed properties.

Considerations Creating Public Properties

There are two implementations of (public) properties:

  • as instance variables.

  • as get-set methods.

From outside the class, you cannot tell the difference between them (for example, both are dynamically bound). However, if you just want to expose an instance variable to users of the class, you can declare a property of that name and type, which declares the instance variable too:

class xxx
     property string StringProp;
end-class

If you want the property to be read-only outside the class, use the following code:

class xxx
     property string StringProp readonly;
end-class

Both of these code examples declare an instance variable called &StringProp and provide its value for getting and change its value for setting (if not readonly). You can think of this as shorthand for the usual implementation of get-set methods.

If you want to implement the same with code (perhaps deriving the property from other instance variables or other data), use the following sort of PeopleCode program:

class xxx
   property string StringProp get set;
private
   instance string &inst_str;
end-class;

get StringProp
   /+ Returns String +/
   Return &inst_str; /* Get the value from somewhere. */
end-get;

set StringProp
   /+ &NewValue as String +/
   /* Do something with &NewValue. */
   &inst_str = &NewValue;
end-set;

To specify a read-only property, omit the set portions of the code example.