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. The lists are broken out between numeric types (integer and float) and non-numeric types.
Numeric Types
These are used to represent integer (whole) numbers. The .NET Framework provides a variety of integer types:
VB Variable Type |
System Type Name |
Size |
Range |
Byte |
System.Byte |
1-byte unsigned integer |
0 to 255 |
Short |
System.Int16 |
2-bytes signed integer |
-32768 to 32767 |
Integer |
System.Int32 |
4-bytes signed integer |
-231 to 231-1 |
Long |
System.Int64 |
8-bytes signed integer |
-263 to 263-1 |
SByte |
System.SByte |
1-byte signed integer |
-128 to 127 |
UShort |
System.UInt16 |
2-bytes unsigned integer |
0 to 65535 |
UInteger |
System.UInt32 |
4-bytes unsigned integer |
0 to 232-1 |
ULong |
System.UInt64 |
8-bytes unsigned integer |
0 to 264-1 |
These are used to represent real numbers (numbers that can have a decimal portion). There are three floating types in the .NET Framework:
VB Variable Type |
System Type Name |
Size |
No. of digits |
Range (approximate) |
Single |
System.Single |
4 bytes |
7 significant digits |
+/- 1.4 x 10-45 to +/- 3.4 x 1038 |
Double |
System.Double |
8 bytes |
15-16 significant digits |
+/- 5.0 x 10-324 to +/- 1.7 x 10308 |
Decimal |
System.Decimal |
16 bytes |
28 significant digits |
+/- 1.0 x 10-28 to +/- 7.9 x 1028 |
The Decimal data type is often used to store currency values.
Non-Numeric Types
VB Variable Type |
System Type Name |
Size |
Range |
Char |
System.Char |
2 bytes Unicode character |
One character |
String |
System.String |
10 + 2 bytes per character |
Up to 2 billion characters |
Object |
System.Object |
4 bytes |
A "supertype" of all types in the .NET Framework (like the variant in VB6). It can contain any type. |
Boolean |
System.Boolean |
2 bytes |
True or False |
Date |
System.DateTime |
8 bytes |
0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999 |
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 |
# |
Decimal |
@ |
Use of type-declaration characters in VB is very “old school”, dating back to the days of BASIC (not Visual Basic or “classic” Visual Basic, but BASIC). The preferred style is to use the "As" clause in a data declaration statement. For example, use Dim strMyData As String instead of Dim strMyData$.
(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 .NET is "happy"; however, many programmers follow stylistic naming conventions in an effort to improve the readability of their code.
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.
It should be noted that the use of Hungarian naming conventions is the subject of much debate. Some programmers are “fans” of it, others feel it is completely and utterly ridiculous and use alternate naming conventions or none at all. My preference is to use the naming conventions discussed below, and these are used in the sample programs provided here. Of course, you can do your own research, and come up with the method that works for you.
The chart below shows commonly used variable types, a recommended three-character prefix, and a sample variable name:
Variable Type |
Recommended Prefix |
Sample Variable Name |
Boolean |
bln |
blnDataIsValid |
Date |
dtm (for "Date/Time") |
dtmEmpBirthDate |
Decimal |
dec |
decBigMoney |
Double |
dbl |
dblAnnSalary |
Integer |
int |
intCounter |
Long |
lng |
lngBigIntValue |
Object |
obj |
objExcelApp |
Single |
sng |
sngHrlyRate |
String |
str |
strMyWord |
Note the style of using a lower-case, three-character prefix, followed by a descriptive name in mixed case. This style of naming variables found in a lot of programming 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