Tech Tip: Store singleton class instance in a variable for convenience in typing
PRODUCT: 4D | VERSION: 20 R6 | PLATFORM: Mac & Win
Published On: October 14, 2024
To get the instance of a singleton class, call its “.me” property. For example, consider the singleton class “MySingletonClass” below:
// Class: MySingletonClass singleton Class constructor() This.greeting:="Hello" This.farewell:="Goodbye" Function myAlertFunction($type : Text) ALERT(This[$type]) |
After instantiation, MySingletonClass’s instance properties and function can be accessed like this:
cs.MySingletonClass.new() cs.MySingletonClass.me.greeting:="Hello 4D" cs.MySingletonClass.me.greeting:="See you later" cs.MySingletonClass.me.myAlertFunction("greeting") cs.MySingletonClass.me.myAlertFunction("farewell") |
However, it can get tedious to type out “cs.MySingletonClass.me” every single time it is needed. Alternatively, the instance can be stored in a variable, like this:
$myVar:=cs.MySingletonClass.me $myVar.greeting:="Hello 4D" $myVar.farewell:="See you later" $myVar.myAlertFunction("greeting") $myVar.myAlertFunction("farewell") |
This way, “cs.MySingletonClass.me” only needs to be typed out once, then on subsequent lines it can be substituted by the variable, making it quicker to access its properties and functions while typing. Notice that on the first line, it can be instantiated and set to a variable at the same time.