Use of global data in an OO world.

Where do I declare ‘global’ data?

I have a dictionary that is used by many classes in my application.
(Its a web app by the way)

I have one instance of this class in WebPage1 but it is used by other classes.

I was reading this ‘conversation’ on stackoverflow…
Dealing with “global” data structures in an object-oriented world

So where in my application should I declare the instance and how would i make it global?

Based on the little experience that I have of RB/Xojo programming, it seems that the “Properties” of all the “Windows” (forms) and “Modules” are global.



You can create a Module and declare its properties as global or protected, or you can add a shared property to a class and access your common data from any instance of that class.

I felt like adding it to WebPage1 as a shared property, but that just felt wrong to be referencing WebPage1.Property a different class…
I think the module is the best solution.

Is there a notion of a constructor in a module?

Thank you.

No constructors in a module it seems.

No since you can’t initiate any instance of a module. It’s not a class, rather think of it as a folder or namespace.

If you need construtors, go with classes. Only there constructors really make sense.

Classes have shared methods and properties as well

Or create an Init method in your module and be sure to call that first.

Be wary of having your class definition code access global data directly. Strive for encapsulation as much as possible. You are right to shy away from ever referring to from within a class. It shouldn’t happen.

An approach that I haven’t seen mentioned above is to put the global data into a class object and then give each of the other classes a reference to that object. That would even work if the “global” object was a protected property of a window. The point is that your class doesn’t care where the “global data” came from, or even how global it actually is.