Академический Документы
Профессиональный Документы
Культура Документы
What is a Variable?
A variable is a meaningful name that takes the place of other data that might change during the course of
your project or macro. Variables temporarily store values while a macro is running. A variable has a name
(the word you use when you wish to refer to the value the variable contains) and a data type (which determines the kind of data the variable can store). You might compare variables to storage containers that you
are familiar with. If you wanted to carry groceries home from the store, you might ask for a paper bag.
What else might a typical paper bag contain? A paper bag with your lunch inside might be carried to work,
or that same paper bag might be used to hold a bottle of vitamins on the way home from the drugstore, or
may hold a candle sitting in sand on some cold night in December (luminaire). Do you care what the bag
contains? No, you simply use it to hold something you wish to take out of it later. Similarly, you might use
a bucket to hold something. What might that same bucket hold on different occasions? It might hold fried
chicken at a picnic, or sand used in making a sand castle at the beach, or soapy water used for washing the
windows of your house or car. What is important about these containers is not the specific items they carry
but the fact that they can carry different types of items. Through experience you will learn which type of
container is most suitable for the task at hand. In a similar fashion, VBA provides containers (variable)
which can be used to hold whatever you desire, simply use the correct container for the task at hand.
Visual Basic for Applications also has a carryall type of variable for use when you are not sure what type of
items you wish to use it for. This variable is called a variant and can be used to hold words, multiple words,
numbers, objects, etc. This wide ranging functionality comes at a price. The Variant data type utilizes the
most memory, this ensures that it is capable of storing virtually any thing you might ask of it. While this
may be tempting to use, especially for beginners, you will be much better served by thinking about what
type of data you are storing and declaring your variable appropriately. For example, a suitcase may carry
your groceries home from the store everybit as well as it carries books home from the library, but just because it can hold many types of items doesnt mean that you should or would want to use it for every occasion. Although that suitcase is the correct container to hold all the clothes necessary for a week long trip, it
may not be best suited for carrying your lunch to work.
PaperSpace
July 2002
Declaring a variable tells the macro about it in advance. Since this represents good coding practice we will
always strive to declare variables in advance of their use. Another aspect of variables is defined as the scope
of the variable. Scope can be defined as the area of the project where that variable can hold a value. For instance, if you declare a variable to be private inside of a sub routine then you cant place a value inside that
variable anywhere but within that sub routine. Variables can have the following scope:
You typically declare a variable with the Dim statement, supplying a name for the variable: (if you make
your variable public then you can declare it by replacing Dim with the word Public)
Dim variablename [As type]
In all of your projects, use global variables only when there is no other way to share data between forms.
When global variables must be used, it is good programming practice to declare them all in a single module,
grouped by function. Benefits from declaring variables in advance include: saving programming time by
reducing the number of bugs caused by typos and minimizing the amount of memory that is used during execution of your macro. Remember that we discussed using the Option Explicit statement in all your forms,
modules, classes, etc.
How are Constants used?
A constant is a meaningful name that takes the place of a number or string that does not change. Although a
constant resembles a variable, you can't modify a constant or assign a new value to it as you can when working with a variable. Often you'll find that your code contains values that reappear over and over without
changing. Or you may find that the code depends on certain numbers that are difficult to remember numbers that, in and of themselves, have no obvious meaning. An example of a constant is the value of pi. You
can greatly improve the readability of your code and make it easier to maintain by using constants.
The syntax for declaring a constant is:
[Public|Private] Const constantname[As type] = expression
(Continued on page 3)
PaperSpace
July 2002
PaperSpace
July 2002
PaperSpace
July 2002
In the next issue we will make this routine more intelligent, more efficient, add some error checking, and
Explore and utilize some of the built in functions made available to us by AutoCAD, and VBA.
PaperSpace
July 2002