In the previous part, I have introduced you to functions and how to use them in your program. These help to improve your program in terms of readability and overall scalability. In this part, I will introduce you to an even more powerful tool in programming, the class.
How to create classes in VBA
Before we go into what are classes, we will first take a look at how to create classes in VBA.
Right click anywhere in the project explorer, click on Insert -> Class Module.
Once you have created the class module, you are presented with an empty code section. This is where you will define your class.
To rename your newly created class, turn on the properties window and type in a new name.
What are classes?
Classes are object constructors. As mentioned in post #3 objects compose of methods and properties. Classes are basically the “blueprints” to create these objects. We will declare these methods and properties within the classes and then call them in our main program to create custom objects that possess these methods and properties.
We will start with a simple example. Imagine a coordinate for a point in 3D space. To describe this coordinate, we will need 3 values: x, y and z coordinate values.
Now let’s say we want to describe the points of a line in this 3D space, we will need two points, and each point requires 3 coordinate values. If we did not have classes, we would have to create a total of 6 variables.
As you can see, this will quickly become a problem for our sanity if we want to create points for a cube for example. We would have to create 24 variables (3 coordinates each for 8 points)! This is where classes will help you out.
Example of a class
So for this example, we can create a new class module named “Coordinate”. And in the code section of the “Coordinate” class, we declare 3 variables “x”, “y” and “z” as type “Double”.
However, instead of using the keyword “Dim”, we will use the keyword “Public” when declaring our variables in this class.
Every method and property in a class can be defined as a “Public” or “Private”. When a method or property is defined as “Public”, we are able to call them from our main code.
With our class defined, we can now create new “Coordinate” objects using our class. In the main part of our program, we declare “point1” and “point2” as “New Coordinate”. We use the “New” keyword here so that the VBA program would generate a new object for us.
As we have declared the properties “x”, “y” and “z” as “Public” we can call them directly in our main program and assign values to them as well.
Class method
We can also include methods in our class. For example, for our “Coordinate” class, we want it to return the coordinate values in the form of an array after we have assigned values to them. We can create a method to do that for us inside of the class.
For example, below we created a function “GetArray” that returns the array of coordinates for us.
To access the method we just call it from our coordinate object.
Summary
Classes are useful in many cases where you need to define certain objects with many properties and also have their own associated methods related to these properties. In this lesson we have explored the basics of creating a class with our own method and properties. In the next part we will explore some examples on using classes with SolidWorks API.