A variable is a name that’s used to refer to a location in memory containing an item of data. Items of data can be anything from strings to whole numbers, from currency to decimal fractions. Figure 4.1 shows the Help window containing all types of data available and their memory requirements.
Figure 4.1 The Help window containing a summary of data types
The Data Type Summary Help window is a useful one to keep on hand; you’ll probably want to look it up a lot until you get used to all the different data types in VBA. The following steps show you how:
1. Open the IDE. Choose Help ^ Microsoft Visual Basic Help if you are using AutoCAD 2000i, or choose Help ^ Contents and Index if you are using earlier versions of AutoCAD. The Visual Basic Reference dialog box appears (or the Help Topics: Visual Basic For Applications dialog for pre-2000i versions) with the I-beam cursor blinking in the text box.
2. Type data types into the text box. The entries scroll by in the list box below, until the “data types” item appears highlighted.
3. Click Display. The Topics Found dialog box appears, as shown here:
4. Select Data Types Keyword Summary and click Display. The Help window appears.
When a variable is declared, the interpreter allocates the exact amount of memory required by its type of data. For example, given the statement
the interpreter would allocate a block of memory four bytes long and initialize it to 0.0. When this variable is assigned a value in a statement, such as CurValue = 5.25, the interpreter stores the 5.25 at this block of memory. When this variable is used in an expression such as
the interpreter program retrieves the value 5.25 from memory.
Variables can be assigned as many different data values as your application requires while it is running, so variables make applications more adaptable than they would be if they used fixed values.
Memory Allocations for String Types
In the Help window for Data Type Summary, notice that the String type is allocated memory according to whether it’s fixed-length or variable-length. Fixed-length strings are allocated 1 byte of memory for each character; variable-length strings are allocated 1 byte for each character, plus 10 bytes. Strings can be declared as fixed-length by specifying the number of characters;for example,the declaration Dim Name As String * 30 ensures that Name is always exactly 30 characters long. If Name is assigned a string less than 30 characters,then trailing spaces are added to make it 30. If Name is assigned a string longer than 30 characters,the string is truncated to 30. Strings that are declared without a specified number of characters are by default variable-length strings.
When VBA encounters a string, it converts each character into a number according to its ASCII representation.The characters in the string "Hello"would be interpreted into five 1-byte numbers in the range 0 to 255. You can see how a string requires a lot more memory than a single integer number.
Declaring Variables Using the Dim Statement
A variable can be declared using the Dim statement with or without specifying its type:
If the type is omitted, then by default Visual Basic uses the Variant type and allocates the maximum amount of memory accordingly. The special Variant type has storage space allocated to it that’s large enough to store data of any of the other types. Any variables that are not declared or that are declared without specifying their type are assumed to be Variant so that they can be assigned just about anything.
Several variables can be declared in the same Dim statement, separated by commas, even if their types are different. For example:
For this reason, every variable that you don’t want to end up as a Variant type must have its type specified in the Dim statement, even if it is declared on the same statement as other variables of the same type. In the following statement:
the variable ScalingFactorX would be interpreted as being a Variant type. This differs from some other languages, including C, in which the type is only specified once at the end of a list of variable names of the same type.
Important Reminder: Naming Conventions in VBA Code
Visual Basic has a few naming conventions that you must adhere to when you’re naming macros, functions, variables, and constants in code:
• Names must always begin with a letter.
• Names cannot contain spaces, periods, or exclamation marks (!),or any of these characters: @ # $ &.
• Names cannot be greater than 255 characters in length.
To Declare or Not to Declare?
As explained, Visual Basic doesn’t require variables to be explicitly declared. If you use this default convention, and if a name appears in code that hasn’t been declared in the statements translated up to that point, a storage location large enough for a Variant type will be allocated to that name. And (whether you want it or not) a new variable is born—and no warning or error messages are given!
You may be tempted not to declare anything and just let Visual Basic do the work for you, but there are two good reasons why you should not automatically go down this road:
• Incorrectly spelled variable names are treated as new variables. That means expressions could be evaluated to an erroneous result. Or, if the new variable is a divisor, it could stop execution of your program with a Division By Zero error message box, as shown here:
• Variables are allocated the maximum amount of memory; even an Integer type variable, which requires 2 bytes if declared, is allocated 16 bytes otherwise. Not only does this blow out the memory requirement of your application very quickly, especially if you are using untyped arrays, but it also increases runtime performance.
To avoid these situations, you can direct the interpreter program to verify that all the variables have been declared before they appear in the code, and if not, to give you an error message.This is done using a special Option Explicit statement.
Option Explicit Statement
The Option Explicit statement placed in a UserForm or module stipulates to the interpreter program that all variables contained in that UserForm or module must be declared; otherwise the interpreter must create an error and stop the application with the message shown here:
The Option Explicit statement is placed in the General Declarations section of the Code window for a UserForm or module. If you want these statements to be added automatically into the General Declarations section of all new UserForms and modules in future projects, you can arrange this by performing the following steps:
1. From the Visual Basic Editor choose Tools ^ Options. The Options dialog box appears.
2. Select the Editor tab. In the list of Code Settings options, select the check box for Require Variable Declaration.
3. Click OK to return to the IDE. Now any new UserForm or modules you insert into any project in the future will open with the Options Explicit statement already in place in the General Declarations section of the Code window.
Any existing UserForms and modules are not affected when you enable the Require Variable Declaration setting.