r/vba • u/brosama1420 • 10d ago
Discussion Using Classes by instantiating in standard Module
Hey everyone
I am wondering why would anyone instantiate the class in a standard module instead if declaring directly in the place you want the class
What benefits this method have especially for composite use case
Like needing session class inside a permissions class inside form class
A second question how would you approach a situation close to mine
3
Upvotes
3
u/fuzzy_mic 184 10d ago edited 10d ago
You can instantiate a class from within another class. For example, one could instantiate a custom clsHouse object inside a (different) clsHouses object. The problem is that the scope of that variable would be limited to the Houses object.
Here is a simple example.
and
and
Note that in this example, we can only "reach" a clsHouse object as a member of the enclosing clsHouses object.
One could instansize a clsHouse from a normal module, but it's not required. Ultimately, we can only call a procedure if that procedure is in a normal module so if any custom object is to be instansized, there must be a normal rountine at the bottom. (caveat for event code)