Best practice for using the aparent class property
The 'APARENT' class property gives access to the instance that references the class you are working on.
If a child class is used by different parent classes, code writing that refers to the parent class in the script associated with the child class can be risky. You can test this.APARENT.objecttype
, but the result will be a fragile code that depends on the parent class evolution.
Example
Suppose you have an "ORDER" class, a "DELIVERY" class, and a "LINE" class.
The "LINES" structure is the same in both cases as it carries the properties DATELINE, QUANTITY, and ITEM.
If you wanted to default the "DATELINE" property with the date found on the header, the incorrect method to use would be to write it in the script associated with the lines.
$PROPERTIES
Case CURPRO
When "DATELINE" : Gosub DATELINE
...
Endcase
Return
$DATELINE
Case ACTION
When "INIT" : Gosub INIT_DATELINE
...
Endcase
Return
$INIT_DATELINE
Case this.APARENT.objecttype :
When "ORDER" : this.DATELINE=this.APARENT.DATEORD
When "DELIVERY" : this.DATELINE=this.APARENT.DATEDEL
Endcase
Return
In summary, THIS CODE IS FRAGILE, DIFFICULT TO READ AND THEREFORE MUST BE AVOIDED.
The correct method to use is as follows:
On the script associated with the "ORDER" class, we will find the following lines:
$PROPERTIES
Case CURPRO
When "LINES.DATELINE" : Gosub DATELINE
...
Endcase
Return
$DATELINE
Case ACTION
When "INIT" : Gosub INIT_DATELINE
...
Endcase
Return
$INIT_DATELINE
this.DATELINE=this.APARENT.DATEORD
Return
$PROPERTIES
Case CURPRO
When "LINES.DATELINE" : Gosub DATELINE
...
Endcase
Return
$DATELINE
Case ACTION
When "INIT" : Gosub INIT_DATELINE
...
Endcase
Return
$INIT_DATELINE
this.DATELINE=this.APARENT.DATEDEL
Return
Summarized, the rule can be expressed as follows:
If the control or the initialization of a property in a child instance does not depend on the parent, place the code at the child class level; otherwise, place it at the parent class level.