[AccessD] Classes and Events - Where are classes initialized and stored?

jwcolby jwcolby at colbyconsulting.com
Tue Feb 24 07:46:35 CST 2009


There might be some confusion about where classes get initialized, and where the pointer to a class 
instance is stored.  We use classes for many different purposes so the answer to this question 
doesn't have a single answer.  Let's take some examples.

We discussed the fact that classes model objects, and sometimes multiple classes are used together 
to model systems of objects.  In one case we looked at clsMsg, which is both an object and a system. 
  The object being modeled is an email message.  The system being modeled is a messaging system.  In 
the case of clsMsg, we created an initialization module where we stored a pointer to a single 
instance of the message class.  In this same module we then created an Init, Term and a function to 
get a pointer to the base object - the clsMsg instance.

As you can see, in this case there is only one instance of the message class, and so we initialized 
it somewhere that any other module could see and use it, in an "initialization module".  Notice that 
once loaded, as long as the APPLICATION is loaded the clsMsg instance will remain loaded.  The 
APPLICATION is tasked with closing the instance when it closes.

We also looked at other instances where the base class was not a public class instance used by 
everyone, but rather a private instance used by only one other object.

As an example the clsFrm that I designed is a base class.  It is instantiated once for each and 
every form that uses clsFrm.  This class might very well have 0 (no forms open) or 1, 10 or 40 
instances loaded at any given time.  Each form opened will load its own instance of clsFrm, so if 
you have a form with a tab with 8 subforms, then you already have 9 instances of clsFrm.  Leave that 
form open and open a second form, also with a tab with 5 subforms, and you now have an additional 
six instances of clsFrm loaded.

In this case, the class instance is dimensioned in the header of the form's "code behind form" 
class, and held open in the form's class.  As long as a given form is loaded, its instance of clsFrm 
is open, and when that form closes, it destroys its clsFrm instance.

As you can see from the examples, classes are loaded on demand by the objects that need them.  WHERE 
the pointer to the class instance is initialized and stored depends on the function of the class.

If a (child) class is used by another (parent) class, and the instance of that child class is used 
only by the parent class, then the child class is instantiated and stored inside of the parent 
class, usually but not always in the class header.  Think about the clsTimer that we discussed.  It 
will be instantiated inside of the function where you are trying to time something.  But again, it 
is used ONLY by that code, and is instantiated, used and destroyed inside of that function.

To summarize, if a class instance is going to be used by multiple other objects, the class instance 
has to be initialized and stored in a location where all of those other objects can get at it.  If a 
class instance is going to be used only by a single parent object, then it is usually instantiated, 
stored and destroyed inside of that parent object.



More information about the AccessD mailing list