Scope of Constants and Variables (VBA Programming Concepts) (AutoCAD VBA)

The scope of a constant or variable refers to whether or not it can be accessed from one procedure, from all procedures in a module, or from all procedures in the application. The level of scope depends on where the element has been declared and whether it has been declared as public or private. Variables and constants can have three levels of scope, as defined in the sections that follow:

•    Procedure level

•    Private-module (or UserForm) level

•    Public-module level

Public vs. Private

All constants and variables are private by default. You can override the default by declaring them as public, provided that the declaration is inside a standard module rather than in a UserForm or procedure. From the point of view of maintaining the code, it is better to avoid declaring a constant or variable as public if it is only going to be used by procedures in the same module. This arrangement limits the amount of code that needs to be checked over if the value of that constant or variable ever needs to be updated.

Scope at the Procedure Level

A constant or variable declared inside a procedure using the Const or Dim statement is considered local and has a lifetime that lasts only while that procedure is running. This means that when the declaration statement is translated, the interpreter reserves the memory for variables or notes the values of constants. When the end of the procedure is encountered, the interpreter frees up the memory reserved for the local variables and no longer remembers the constants’ values.


Although local variables and constants are restricted to being private, you can’t declare them using the P ri vate keyword. If you try to do this, you’ll get a run-time error message "Compile Error – Invalid attribute in Sub or Function. "

UserForm and Module-Level Scope

Any constant or variable declaration that is placed outside a procedure has UserForm or module-level scope. These declarations are placed in the General Declarations section of the Code window and are private by default.

Any constant or variable declared in a UserForm is restricted to being private. If the declaration is inside a module, the constant or variable is private by default but can be declared using the Public keyword to make it accessible from anywhere in the project.

Scope at the Private-Module Level

A module-level constant or variable can be declared as private, in order to limit its accessibility to procedures contained in the same module. For example:

tmp757e-138_thumb

Limiting the scope in this way is useful when it comes to maintaining your code. In this example, if you ever want to change the value of the maximum height, you can quickly see where the constant is used in your code. Another benefit is that you can have another MaximumHeight constant declared as private in another module, and it can have a different value without causing any adverse effects on other modules.

Scope at the Public-Module Level

A module-based constant or variable can be declared as public, in order for it to be referenced from anywhere in the entire project. For example:

tmp757e-139_thumb

In addition to scope, a variable also has a lifetime, which refers to the period of time that they hold onto their value. A variable declared inside a procedure can only be accessed within that procedure, so its lifetime will end when it loses its value between calls unless it is declared as a static variable (see the next section).

Static Statements and the Static Keyword

A variable declared using the Static statement is a local variable that retains its memory allocation for the whole time the application is running. When a procedure is revisited, the variable is still set to the same value that was last assigned to it.

At the procedure level, the Static keyword is used to preserve the memory values of all a procedure’s variables between visits. Listing 4.1 shows the code for a small application that demonstrates the difference between static procedures and static variables. Exercise 4.1 demonstrates how the TrueFlag variable keeps its value between runs, while the Incremental variable does not.

Exercise 4.1: Demonstration of Static Keyword’s Effects on the Lifetime of Variables

If you would like to try these procedures out, the following steps show you how to develop the GUI needed to run them:

1.    Start AutoCAD, open the IDE, and choose Insert ^ UserForm.

2.    Drag and drop two text boxes and two command buttons onto the UserForm, starting from the top, as illustrated in Figure 4.4.

3.    Change the Caption properties of the command buttons in the Properties window to Switch It No 1 and Switch It No 2. (You may need to execute View ^ Properties Window if the Properties Window is not already displayed.)

4.    Enter the code shown in Listing 4.1 into the form’s code window.

5.    Select Run < Run Sub/UserForm.

 The UserForm with two text boxes and two command buttons

Figure 4.4 The UserForm with two text boxes and two command buttons

6.    Repeatedly click the Switch It No 1 button. The static procedure runs with each click, and the top text-box switches between True and False, while the value in the second text-box increases by five each time.

7.    Repeatedly click the Switch It No 2 button. The SwitchIt2 procedure runs with each click, and the top text-box switches between True and False, because TrueFlag is declared as a static variable and so retains its previous value. The value in the second text-box remains at 5 because the Incremental variable is initialized to zero every time the SwitchIt2 procedure runs (because the Incremental variable is allocated a fresh memory location each time).

Listing 4.1: Application with Static Procedures and Variables

Listing 4.1: Application with Static Procedures and Variables

Listing 4.1: Application with Static Procedures and Variables

Analysis

•    Line 1 declares procedure SwitchItl using the Static keyword, so that it holds onto all the memory allocated to its variables as long as UserForm1 stays loaded.

•    Line 2 uses the Dim statement to declare the variable TrueFlag as a Boolean type. This will be assigned the default value False.

•    Line 3 uses the Dim statement to declare the Incremental variable as an Integer type. This will be assigned the default value zero.

•    Line 4 toggles the value of TrueFlag to False if it is True, or to True if it is False.

•    Line 5 assigns the value of TrueFlag to the Text property of TextBoxl, to display it to the user.

•    Line 6 adds 5 to the value stored at the memory location referred to by the Incremental variable.

•    Line 7 assigns the value referred to by Incremental to Textbox2’s Text property to display it.

•    Line 8 ends the static procedure SwitchItl, but the storage locations allocated to the TrueFlag and Incremental variables are retained intact for the next call to SwitchItl.

•    Line 10 declares SwitchIt2 as a normal, run-of-the-mill procedure, so any variables it contains that are not declared as static variables themselves will actually lose their values when the procedure finishes executing.

•    Line 11 declares the TrueFlag variable using the Static statement so that TrueFlag will hold onto its memory location and value for the whole time that UserForm is loaded.

•    Line 12 uses the Dim statement to declare the Incremental variable as an Integer type and sets its value to zero.

•    Line 13 toggles the value of TrueFlag from True to False and vice versa.

•    Line 14 displays the value of TrueFlag in TextBox1.

•    Line 15 adds five to the value of Incremental, which has just been set to zero—this has the same effect as assigning a 5 to Incremental each time.

•    Line 16 displays the value of Incremental in TextBox2.

•    Line 17 ends procedure SwitchIt2 and frees the storage location allocated to Incremental.

•    Line 19 starts the Click Event procedure of the command button with the caption “Switch It No 1.”

•    Line 20 calls SwitchIt1 to perform the application’s response to the click.

•    Line 21 ends the Click event.

•    Lines 23 through 25 are similar to lines 19 through 21 but call SwitchIt2 in response to the Switch It No 2 button’s being clicked.

When a UserForm is open, you can unload it by ending the application or by inserting the statement UnloadUserForm1 when UserForm1 is its name, or more generically as Unload Me when it is the active UserForm.

Next post:

Previous post: