VBA Class Create
Declaration
To create an instance of a class, two lines of code are needed. Say the name of the class is cLogger
.
1 2 3 | Dim Logger As cLogger Set Logger = New cLogger |
The first line declares a variable of the type cLogger
.
The second line assigns an object reference to the variable. Now, the variable is an instance of the class, and you can use it in your code.
The second line will also execute the public sub Class_Initialize
. This is a very interesting feature of classes that does not exist for other datatypes.
If you forget the Set ... = New ...
statement, the class instance will be Nothing
, and, cannot be used meaningfully. The VBA editor will not warn you that you forgot the second line. The code will compile but an error will be generated at run time.
1 2 3 4 | Dim Logger As cLogger 'Set Logger = New cLogger Logger.ToLog "INFO: Main has started" |
The sub Class_Initialize()
The sub Class_Initialize
is a public sub that will be called automatically every time an object reference is assigned to a variable of the type defined by the class. Or, in other words, every time an instance of the class is created.
Typically, the sub Class_Initialize
is used to put default values in the variables of the class although the code in it can be as complex as you desire. In more advanced languages, the constructor - as this is the common name object oriented coders will use for this sub - allows for cool code designs (like singletons).
The sub is Public
and, consequently, part of the class interface in VBA. Despite that it beging part of the class interface in VBA, it is probably best to see the sub Class_Initialize
as a pure event routine and never call it explictely. In other programming languages you will see that the routine will often not be part of the class interface and cannot be called explicitely.
In VBA, the sub Class_Initialize
cannot have arguments.
If you would like to test, put a messagebox in it. Each time the sub is executed, the messagebox will appear and you will see each time the sub is executed.
Why is the sub Class_Initialize
important?
Have a look at the alternative.
The alternative is that you create, in the class, a Public
sub with the same code as in the sub Class_Initialize
. Let's call this sub Initialize
.
Here is how, in this case, you would correctly declare an instance of the class. Note the third line.
1 2 3 4 | Dim Logger As cLogger Set Logger = New cLogger Logger.Initialize |
If the third line is there, the code will behave exactly the same as with the sub Class_Initialize
. However, your class documentation will need to be very clear on the fact that the class should always be intialized before use. Whatever you try, at some point, somebody is bound to forget.
The added value of sub Class_Initialize
lies in the fact that it is called automatically. It cannot be forgotten. It cannot be bypassed. You, as a designer of the class, are sure that any class instance will always be intialized correctly and the user of the class has one less thing to remember. The organization of the code is better, facilitating everybodies coding life.
Declaration in one line
A class instance can also be declared in one line. This is called auto-instancing.
1 2 | Dim Logger As New cLogger |
There is a subtile difference, compared to the two lines declaration. In the one line declaration, the object reference will be assigned in the next line with executable code that involves the variable Logger
. In other words, the object reference will be assigned the first time you use the variable.
1 2 3 4 | Dim Logger As New cLogger MsgBox "Just a message." 'Logger is still nothing Logger.ToLog "INFO: Main has started" |
The line Logger.ToLog "INFO: Main has started"
assigns the object reference and executes the ToLog
sub. If the object gets destroyed, it will be reassigned in the next line with executable code that involves Logger
.