Methods on User-Defined Object Types
- Last UpdatedOct 29, 2024
- 2 minute read
When you define a new object type, you can also define methods which can be used to handle all objects created with the new type. PML method definitions are stored in the same file as the object definition, after the endobject command.
Here is an example of an object which defines three methods to illustrate the main ideas.
Note:
Within a method, !This represents the object which invoked the method and !This.Answer is the way to refer to member Answer of this object.
|
The Code |
What It Does |
|
define object LIFE member .Answer is REAL endobject |
Defines the object, with a single member Answer. |
|
define method .Life() !This.Answer = 42 endmethod |
Defines a method with no arguments but the same name as the type of the object is called the default constructor method. If the default constructor method is present, PML will call it automatically to initialize the object whenever an object of that type is created. |
|
define method .Answer() IS REAL return !This.Answer endmethod define method .Answer( !Value Is REAL) !This.Answer = !Value endmethod |
A method may return a result in just the same way as a PML function using the return command. Set a member of the object using !This.membername. |
These methods might be used in the following way:
!Marvin = object LIFE()
-- The method .Life() was called automatically
!Number = !Marvin.Answer()
-- !Number is set to the value 42
!Marvin.Answer(40)
!Number = !Marvin.Answer()
-- !Number now has the value 40
Warning:
When you create a new object type, or change an existing definition, you must load
the definition by giving the command: pml reload object _name_.