Microsoft Office Tutorials and References
In Depth Information
Table 5-3. Naming Convention for Standard Variables
is perfectly legal, but 1000 lines of code and six months later, will we remember which invoice is
chrt1 and which is chrt2 ? Since we went to the trouble of naming the charts in a descriptive
manner, we should do the same with the Chart variables, as in:
Dim chrtSales As Chart, chrtTrans as Chart
Set chrtSales = Charts("Sales")
Set chrtTrans = Charts("Transactions")
Of course, there are exceptions to all rules, but, in general, it is better to choose descriptive names
for variables (as well as other items that require naming, such as constants, procedures, controls,
forms, and code modules).
Second, a variable name should reflect something about the properties of the variable, such as its
data type. Many programmers use a convention in which the first few characters of a variable's
name indicate the data type of the variable. This is sometimes referred to as a Hungarian naming
convention, after the Hungarian programmer Charles Simonyi, who is credited with its invention.
Table 5-3 and Table 5-4 describe the naming convention that we will generally use for standard
and object variables, respectively. Of course, you are free to make changes for your own personal
use, but you should try to be reasonably consistent. These prefixes are intended to remind us of the
data type, but it is not easy to do this perfectly using only a couple of characters, and the longer
the prefix, the less likely it is that we will use it! (Note the c prefix for integers or longs. This is a
commonly used prefix when the variable is intended to count something.)
Table 5-3. Naming Convention for Sta ndard Variables
Variable
Prefix
Boolean
b or f
Byte
b or bt
Currency
cur
Date
dt
Double
d or dbl
Integer
i, c, or int
Long
l, c, or lng
Single
s or sng
String
s or str
User-defined type
u or ut
Variant
v or var
Table 5-4. Naming Conventio n for Some Object Variables
Variable
Prefix
Chart
ch or chrt
Workbook
wb or wbk
Worksheet
ws or wks
Pivot Table
pt or pvt
Font
fnt
Range
rng
In addition to a data type, every variable has a scope and a lifetime . Some programmers advocate
including a hint as to the scope of a variable in the prefix, using g for global and m for module
level. For example, the variable giSize is a global variable of type Integer. We will discuss the
 
 
Search JabSto ::




Custom Search