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 determination
    Fmethod XX011ACCESS()
    Local Integer ARET_VALUE
    Gosub XX011ACCESS From WMC0XX011DE
    End [L]ARET_VALUE

(Name/code of the method is ‘XX011ACCESS’ and the return value declared as an integer.)

To use such a method from a different class requires several program statements as seen in the ‘Example’ code below. In summary:

  1. Declare an Instance of the class that includes the method to use.
  2. Allocate that Instance; the method is now ‘loaded’ and available to use.
  3. Call the method with the operator 'fmet'. As the method is in a different class, the call is prefixed with the instantiated class.
  4. Declare a local variable for the return value if necessary. The return value type is the one declared for the method in the original class.
  5. When the class is no longer needed, you should free the memory it used.

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
    $XX011ACCESS
    Gosub DECLARE_VAR    
    [L]ACTION = "XX011ACCESS" : [L]CURPRO = "" : Gosub ACTION From ASYRSUB    
    Return

Example

    # Declare variable for return value
      Local Integer ACCESS_LEVEL
    # Declare instance of the needed class
    # Allocate instance
      Local Instance DE Using C_XX011DE
      DE = NewInstance [DE], C_XX011DE
    # Execute method
      ACCESS_LEVEL = fmet DE.XX011ACCESS()
    # Free memory
      FreeInstance DE
Respectively, in a new notation with an allocation group,
    # Declare variable for return value
      Local Integer ACCESS_LEVEL
    # Declare instance of the needed class
    # Allocate instance
      Local Instance DE Using C_XX011DE
      DE = newinstance C_XX011DE AllocGroup Null
    # Execute method
      ACCESS_LEVEL = fmet DE.XX011ACCESS()
    # Free memory
      FreeGroup DE