VBA – Usage of Classes and Properties

VBA is an object-oriented language. As such, it is quite OK to create classes and to assign properties to these classes there. In the current article I will show how to create a class “clsPC” with three properties – Processor, Video and Price. Each property has a get and let. The idea is that the get takes the value, assigned to the property and the let assigns value to the property. Easy.

E.g., if you want to name a dog, you may name it “Lucy”, using the let property and whenever you want to know its name, you may use the get property to tell it to you.

This is how the clsPC looks like:

A little hitch – I have added some code to be printed, whenever the class is initiated or terminated. So, whenever we have the following line:

set someVariable = new clsPC

we would have clsPC is initiated printed in the immediate window. In the code below I create two PCs and I assign properties to them. Then I add them to a collection and I print the properties. At the end I empty the two objects and I have “Terminated” printed. This is how my code looks like:

At the end, it generates the following in the immediate window:

ClassesVBA

Enjoy it! 🙂

Tagged with: , ,