How to use methods defined in other classes
This ‘How-to’ describes how to use a method defined in a class in another script or program.
Sometimes a method defined in a class has functionality needed in other scripts or programs.
In such cases, you do not have to copy and add the method to the scripts or programs that need them; they can be used from the classes they were originally defined in.
Methods defined in other classes you want to use in your own script must be defined as a method in those classes.
The program to define a class ‘GESACLA’ has a ‘Methods’ tab with a ‘METHODS’ section. The methods must have been declared with their code, description, and return type.
When a class is validated, a structure source (*.stc) is generated that includes the call of these methods.
For a class called 'XX011DE' the structure source is 'C_XX011DE.stc' and for a method 'XX011ACCESS' the method code is similar to the example below.
C_XX011DE.stc##################################################################### Methods of the class##################################################################### Fmethod Access level determinationFmethod XX011ACCESS()Local Integer ARET_VALUEGosub XX011ACCESS From WMC0XX011DEEnd [L]ARET_VALUE
To use such a method from a different class requires several program statements as seen in the ‘Example’ code below. In summary:
When the operator 'fmet' performs the method, first its code within the structure source is executed (see example Fmethod XX011ACCESS above). This code in turn executes a method in another, generated source whose name starts with 'WMC0' and ends with the class code (in our example 'WMC0XX011DE'). The name of the method is the same as the original.
Finally, as shown in the method below, the name of the original method is assigned to '[L]ACTION' and executed with the 'Gosub' operator. WMC0XX011DE.src##################################################################### Methods of the class##################################################################### Fmethod Access level determination$XX011ACCESSGosub DECLARE_VAR [L]ACTION = "XX011ACCESS" : [L]CURPRO = "" : Gosub ACTION From ASYRSUB Return
# Declare variable for return valueLocal Integer ACCESS_LEVEL# Declare instance of the needed class# Allocate instanceLocal Instance DE Using C_XX011DEDE = NewInstance [DE], C_XX011DE# Execute methodACCESS_LEVEL = fmet DE.XX011ACCESS()# Free memoryFreeInstance DE
Respectively, in a new notation with an allocation group, # Declare variable for return valueLocal Integer ACCESS_LEVEL# Declare instance of the needed class# Allocate instanceLocal Instance DE Using C_XX011DEDE = newinstance C_XX011DE AllocGroup Null# Execute methodACCESS_LEVEL = fmet DE.XX011ACCESS()# Free memoryFreeGroup DE