In the previous article in this series, I have given an overview of what SolidWorks API is and its capabilities. In this lesson, we will start going into the code and learn about variables, declaration and assignment of variables.
Declaring Variables
Whenever we create a program, it is inevitable that we will have to use variables. Variables are just identifiers that points us to some piece of data that we can read or modify. To get a better understanding, let’s look at the following code.
In the first line, we can see that a variable has been declared with the type “Object”. In VBA, the word “Dim” is a keyword used for declaring variables. Whatever that is typed after the “Dim” keyword would be the variable name or identifier. In this case the identifier is set as “swApp”, but it can be anything as long as it is not one of the keywords in VBA. After the identifier, we can see the keyword “As”. This keyword is to assign the type to the variable. In this case, the type of “Object” is assigned to the variable “swApp”.
So after this first line of code, the program now knows that, a variable with the name of “swApp” exists. And this variable has a type of “Object”. But although this variable exists, no value has been assigned to it yet. If we were to ask the program to tell us the value of “swApp”, it would literally say nothing. So we can think of variables as this, a container that stores values of some type. As declaring a variable does not involve assigning a value, what is created would merely be an empty container.
Now you may be wondering what is an “Object” type, and what exactly is a type. To put it simply, a type is used to tell the computer how to store data at the binary level. Data can come in many forms, from the basics such as integers, decimals, alphanumerical letters (known as characters or strings). The computer needs to know exactly what kind of data you are trying to store in a variable, and allocate the appropriate amount of memory to store the data. I will go into more details on data types and structures, as well as how to define your own types in a future article.
The “Object” data type in VBA is actually a versatile data type. It acts like a wildcard in a way. What the “Object” data type actually stores is an address that points to data of any type. This way, you can use “Object” when you do not know at compile time what data type the variable might point to. Although convenient, it is still best practice to choose the appropriate type when declaring variables as this would improve clarity when reading your code. Imagine trying to find out what kind of data a variable you have declared previously, only to see that it is “Object” and could literally mean any other type. Declaring the proper type also helps VBA or the IDE you are using to suggest member objects for autocompletion. The topic for member objects will be discussed in a future topic.
Assigning Value to Variable
Now that a variable “swApp” has been declared as an “Object”, we want to assign something to this variable. To do that we will do that with a line of code with the “Set” keyword as shown highlighted in the image above. What the “Set” keyword does is assigning the reference of a data to the variable. In most cases, the “Set” keyword is used whenever we are assigning an object to a variable.
Notice that the assignment line of code is placed between the “Sub main()” and “End Sub” lines of code. The “Sub” keyword in VBA represents the declaration of a function. The line “Sub main()” is a special function in VBA compared to any other function. Whenever a VBA program is run, the “Sub Main()” function is always called once after initialization. This is the only function that behaves this way, all other function needs to be explicitly called by the user in the code. This means that as this VBA program is run, the running instance of SolidWorks is assigned to the variable “swApp”. The variable “swApp” is now able to provide us access to all the functionality within SolidWorks and will be the most important variable we have.
Another thing to note is that while the assignment line of code takes place within the “Sub main()” function, the declaration line of code is placed above the “Sub main()” line of code. What this means is that variables can be declared anywhere within the code. However, assignment of values to the declared variable can only take place within a function. This is something to take note about the rules of programming in VBA.
Not all variables are assigned values using the “Set” keyword however. Variables declared with basic or elementary data types, such as integers, decimals, characters or strings, do not use the “Set” keyword. Their value can be assigned directly. Whenever users forget to use the “Set” keyword, the following error would display when running the code.
So whenever you see an error like this, check if you are missing a “Set” keyword for any of your variables.
Summary
In this article, we have covered on how to declare variables, assign values to variable and other useful information regarding variables. In the next article, I will explain about the SolidWorks Object Model, one of the most important topics to learn before using SolidWorks API.