Microsoft Office 2000/Visual Basic Programmer's Guide   

Variable Names

As mentioned earlier, variable names use the mixed-case form (Noun or NounVerb), specifying what the variable is and what it does. This is the descriptive portion of the variable name where the first letter of each word is in uppercase and the rest is in lowercase.

Variable names also have a two- or three-character prefix used to specify the variable's data type. For example, the following statements declare variables with a prefix that specifies the variable's data type:

Dim strRecipientName          As String
Dim intItemsProcessed         As Integer
Dim blnContinueProcessing     As Boolean

The two-character prefix is typically used to specify an Office Application object type. For example:

Dim xlApp          As Excel.Application
Dim olNameSpace    As Outlook.NameSpace
Dim wdNewDoc       As Word.Document

Use the "obj" prefix when declaring a generic, or late-bound, object variable. Use this prefix even when you are creating a late-bound object variable that represents an Office application. For example:

Dim objXLApp       As Object
Dim objWDDocument    As Object
Dim objOLMailItem    As Object

Note   Many developers who have worked with the BASIC language for years have developed the habit of specifying data types by using a single-character suffix; for example:

Dim FName$
Dim i%
Dim Value&

Although BASIC still supports these data-type suffixes, their use is no longer recommended.

Global and module-level variables use an additional single-character prefix to specify their scope. The scope of a variable defines its lifetime and visibility. Global and module-level variables have a permanent lifetime. That is, the memory allocated to the variable remains allocated as long as the application is running. Variables declared within a procedure (except when they are declared by using the Static keyword) are visible only within the procedure where they are declared and have a lifetime that lasts only so long as the code within the procedure is executing.

Global variables have a lowercase "g" prefix and are declared in the Declarations section of a module by using the Public statement. They are visible to all procedures in all modules in an application. For example, Public gstrPathToDataSource As String would be a global variable that contains a string that is the path to the data source used in the application.

A Note on Global Variables

Variables should always be defined using the smallest scope possible. Use global variables only when there is no other way to share the data they contain. Global variables can make your code hard to understand and difficult to maintain. If you find you are using more than a few carefully chosen global variables, you probably need to redesign your code to eliminate them.

Module-level variables have a lowercase "m" prefix and are declared in the Declarations section of a module by using the Dim or Private statement. They are visible to any procedure within the module in which they are declared. For example, Dim mrstCustomerRecords As ADODB.Recordset would be a module-level object variable for customer records. In class modules, module-level variables that are declared with the Private statement have a "p_" prefix. Public module-level variables in class modules appear as properties of the class and should not have any prefix to indicate their data type or scope. For more information about naming variables in class modules, see Chapter 9, "Custom Classes and Objects."

Procedure-level variables are created within a procedure by using the Dim statement. For example, Dim intCurrentMailItem As Integer would be a procedure-level variable used as a loop counter. Procedure-level variables can also be declared by using the Static keyword. Static variables retain their value even after the procedure in which they are declared has finished running. Static procedure-level variables have a lowercase "s" prefix. For example, Static scurTotalSales As Currency would create a procedure-level static variable used to keep an accumulating total in a procedure that calculates current sales.

User-defined type variables are declared in the Declarations section of a module by using an all uppercase type name with "_TYPE" appended to the type name. You could declare a user-defined type in the following manner:

Type EMPLOYEEINFO_TYPE
   strFullName As String
   lngEmployeeID As Long
   datStartDate As Date
   strDepartmentCode As String * 4
   curSalary As Currency
End Type

You would declare a module-level variable of type EMPLOYEEINFO_TYPE by using a "udt" prefix. For example, Dim mudtEmployeeRecord As EMPLOYEEINFO_TYPE.

Array variables have a lowercase "a" prefix and, unless the variable is a variant, are followed by a pair of parentheses. Array variables are declared by using the Dim statement; for example, Dim alngNum() is an array variable of type Long. An array is a variable that can contain multiple values. Arrays are useful when you need to store a number of values of the same type, but you don't want to create individual variables to store them all. For more information about working with array variables, see Chapter 7, "Getting the Most Out of Visual Basic for Applications."

Here are some examples of variable names that use the general naming guidelines described earlier.

Variable Data type Scope
strCompanyName String Procedure
rstCurrentOrders Object Procedure
intCurrentRecordCount Integer Procedure
wdWordApplication Object Procedure
varClipboardData Variant Procedure
curAmountPastDue Currency Procedure
blnProcessNextRecord Boolean Procedure
molOutlookMailItem Object Module
mcolCurrentUsers Object Module
gcnnDBConnection Object Global
gstrLogonID String Global
gastrContactNames() String (array) Global