Variables – Part 1
Data Types and Naming Conventions
A variable is simply a name you give to an area of memory in which a data value used by your program is stored. When you need to retrieve that data, or modify its value, you can refer to the memory location by the variable's name.
The VB variable types, as well as the range or types of values they can store, are listed below:
Variable Type |
Description |
Boolean |
2-bytes, can contain the special values True or False |
Byte |
1-byte, unsigned integer 0 to 255 |
Currency |
8-byte fixed-point. Stores 4 digits after the decimal point. Stores approx. +/- 922 trillion. |
Date |
8-byte date/time value from 1/1/100 to 12/31/9999. Additional Info: Dates are stored internally as eight-byte Double Precision floating point variables which can represent dates from January 1, 100 A.D. up to December 31, 9999. The integer portion holds the date with zero being December 30, 1899. Dates prior to this are stored as negative values; those after as positive. The February 12, 1997, for example, is stored as 35,473. The fraction portion holds time information. Midday, for example, is represented as 0.5. |
Double |
8-byte floating point number (-1.79E+308 to 1.79E+308) |
Integer |
2-byte integer, stores approx. +/-32K |
Long |
4-byte integer, stores approx. +/-2 billion |
Object |
A 32-bit (4-byte) address that refers to the location of an object. |
Single |
4-byte floating point number (-3.40E38 to 3.40E+38) |
String |
Variable-length: 10 bytes + string length, holds up to 2 billion chars Fixed-length: holds up to 64K chars |
Variant |
Can hold any native data type, object reference, and the special values Error, Empty and Null. Numeric variants require 16 extra bytes, string variants require an extra 22. |
The rules for forming a valid VB variable name are as follows:
(1) The first character must be a letter A through Z (uppercase or lowercase letters may be used). Succeeding characters can be letters, digits, or the underscore (_) character (no spaces or other characters allowed).
(2) The final character can be a "type-declaration character". Only some of the variable types can use them, as shown below:
Data Type |
Type Declaration Character |
String |
$ |
Integer |
% |
Long |
& |
Single |
! |
Double |
# |
Currency |
@ |
Use of type-declaration characters in VB is not encouraged; the modern style is to use the "As" clause in a data declaration statement (described below).
(3) The name can contain a maximum of 255 characters.
(4) The name cannot be a reserved word (VB keyword).
VB is not case-sensitive: PAY, Pay and pay all refer to the same variable. If you type a variable on a line in a case other than the case you used when you declared the variable, VB will change the case to match that of the declared variable when you leave that line.
Variable Naming Conventions
As long as you stick to the rules above, VB is "happy"; however, many programmers follow stylistic naming conventions in an effort to improve the readability of their code. The naming conventions involve using a one-to-four character lower-case prefix to start off the name. The purpose of the prefix is to identify the data type of the variable (like "int" for integers and "str" for strings). These naming conventions are sometimes called "Hungarian naming conventions" (after Charles Simonyi, an early Microsoft programmer – and Hungarian native – who proposed the conventions). Two other prominent programmers, Leszynski and Reddick, also have led the charge for naming conventions.
The chart below shows each variable type, a recommended three-character prefix, and a sample variable name:
Variable Type |
Recommended Prefix |
Sample Variable Name |
Boolean |
bln |
blnDataIsValid |
Byte |
byt |
bytSmallNum |
Currency |
cur |
curBigMoney |
Date |
dtm (for "Date/Time") |
dtmEmpBirthDate |
Double |
dbl |
dblAnnSalary |
Integer |
int |
intCounter |
Long |
lng |
lngBigIntValue |
Object |
obj |
objExcelApp |
Single |
sng |
sngHrlyRate |
String |
str |
strMyWord |
Variant |
vnt (or "var") |
vntWhatever |
Note the style of using a lower-case, three-character prefix, followed by a descriptive name in mixed case. This is the style of naming variables found in most VB documentation.
It is also recommended that you indicate the scope of the variable with an additional one-character prefix in front of the variable name. My recommendations are:
In addition, it is recommended that the letter a be used to indicate an array name. In an array name, the letter "a" would appear before the variable type indicator, but after the scope indicator. For example, an array of Boolean switches might be named as follows, depending on where it is declared:
ablnSwitches local level
mablnSwitches module level
gablnSwitches global (project) level
pablnSwitches passed parameter