KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Shared singleton class setter function
PRODUCT: 4D | VERSION: 20 R5 | PLATFORM: Mac & Win
Published On: July 22, 2024

When creating a singleton class, it is useful to have a setter function, so that properties can be added or updated. If the singleton is a shared singleton, the Use/End use keywords are not required when setting properties. Simply place the shared keyword in front of a function. Take for example the following class named “ip”:

shared singleton Class constructor()

shared
 Function set($name : Text; $value : Variant)
    This[$name]:=$value

This singleton can be used to store variables that are shared between processes. For instance, when a “foo” variable with value “bar” needs to be added, use the set function:

cs.ip.me.set("foo"; "bar")

Then, get the value for that variable in a different process by simply accessing the singleton’s “foo” property:

$foo:=cs.ip.me.foo // "bar"