Variables are more complicated to name because the <prefix> portion needs to convey more information. We use a (very!) modified Hungarian approach (probably closer to Czech), where the first character indicates the scope, a variable's persistence (static), and whether a procedure argument is passed by reference or by value, according to the following table:
Table 5: Scope, persistence, how passed
Scope/Persistence/How passed |
Letter |
Local |
l |
Module (form) |
m |
Global |
g |
Static |
s |
Passed by reference |
r |
Passed by value |
v |
For data variables, we use a two-character prefix, the second of which identifies the data type. We review these in Table 6, and show some examples combining the scope/persistence/how passed and data type portions of the prefix.
Table 6: Data variable naming in Visual Basic
Prefix |
Data Type |
Example |
c |
Currency |
lcSalary |
d |
Floating point double |
gdPi |
f |
Floating point single |
gfMolecularWeight |
l |
Long integer |
vlCustomers |
i |
Integer |
liCounter |
s |
String |
vsReturnValue |
v |
Variant |
lvDate |
Object variables are of special importance in Visual Basic, since so much of our code needs to declare and manipulate them. Since there are so many more specific (and general) object types than there are conventional data variables, we use a three character identifier, which comes after the scope/persistence/how passed portion of the prefix, to identify the object type. Table 6 is not a complete list, but it gives enough detail to indicate how we distinguish general from specific object types, and how we distinguish object types that are defined by Visual Basic as opposed to those defined by the JET engine. The latter we generally refer to as data access objects, or DAO's for short. Since collections cannot be pointed to by variables in Visual Basic 3.0, we have indicated that with an "NA" prefix in a couple of examples. Also, note that we have included both generic and specific objects in the table. We'll discuss this below in the section on optimization, but whenever possible, you should use the specific object type in preference to the general.
Table 7: Naming object variables in Visual Basic
Prefix |
Object |
Example |
Generic Visual Basic objects |
||
frm |
Form |
mfrmCurrent |
NA |
Forms collection |
NA |
ctl |
Control |
lctlCustomerGrid |
NA |
Controls collection |
NA |
Specific Visual Basic objects |
||
dat |
data control |
mdatCustomers |
img |
image control |
rimgClaimForm |
txt |
text box |
ltxtCustomerID |
grd |
grid control |
rgrdCurrent |
JET 2.x Engine objects |
||
rsd |
Dynaset type recordset |
rrsdCurrent |
rss |
Snapshot type recordset |
lrssOrders |
rst |
Table type recordset |
mrstOrderDetails |
dba |
Database |
mdbaSales |