Tech Tip: Dynamically call user class functions
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: September 12, 2023
When writing a user class, sometimes a function needs to dynamically call other functions from the same class. This can be done by invoking the functions with the “This” keyword and bracket notation. As a generic example, consider the class “MyClass”:
Class constructor This.actions:=["doA"; "doB"; "doC"] This.output:="" Function doA This.output+="A" Function doB This.output+="B" Function doC This.output+="C" Function runActions($signal : Text) For each ($action; This.actions) If (($signal="skipB") && ($action="doB")) continue Else This[$action]() End if End for each return This.output |
The function “runActions” will iterate the above actions and execute each function dynamically using “This[$action]( )”, unless “$signal” is passed to skip one function. Example of the class in action:
$obj1:=cs.MyClass.new() $obj2:=cs.MyClass.new() $res1:=$obj1.runActions() // ABC $res2:=$obj2.runActions("skipB") // AC |