Hello all,
Is there a way to create a globally-accessible instance of a class? I can use Modules with global properties and global methods, but I’d rather use a global class instance if possible.
Thanks,
Andy
Hello all,
Is there a way to create a globally-accessible instance of a class? I can use Modules with global properties and global methods, but I’d rather use a global class instance if possible.
Thanks,
Andy
Seems this should be same as any other data type defined as property in any module that becomes a global variable. Have not tried this yet to confirm the same.
What are the details of the difficulty being faced please?
[quote=116448:@Syed Hassan]Seems this should be same as any other data type defined as property in any module that becomes a global variable. Have not tried this yet to confirm the same.
What are the details of the difficulty being faced please?[/quote]
Thats right, just declare it in a module as a property.
Should there only ever be 1 instance of the class or multiple instances and you just want access to one in particularly globally?
Let’s say you have a class “MyClass”, and you want a global instance of it. Create a Constructor and make it Protected. This will keep it from being instantiated elsewhere. Then create a Shared Property, mInstance As MyClass, and make it private. Create a Shared Method, GetInstance() As MyClass. The code behind GetInstance is:
if mInstance is nil then
mInstance = new MyClass
end if
return mInstance
Elsewhere in code, you will access this instances as MyClass.GetInstance.
To take it one step further, add a method to a Module, myInstance() as MyClass. The code is
return MyClass.GetInstance
You can now use myInstance as a global variable that refers to a singleton instance of MyClass.
myInstance.someProperty = "Fred"