VBA Class: How to create

VBA Class Create


To create an instance of a class, two lines of code are needed. Say the name of the class is cLogger.

The first line declares a variable of the type cLogger.

Image showing to declare a class

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.

Image showing to assign object reference

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.

Image showing VBA error object not set

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.

Image showing to assign object reference

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.

Image showing a message box in Class_Initialize

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.

Image showing a public sub Initialize

Here is how, in this case, you would correctly declare an instance of the class. Note the third line.

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.

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.

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.

Hard to remember, no? We never create using the Dim ... As New ... statement.