Visual Basic

Scope Information

We also use prefixes to denote an object's scope. Our scope prefixes come between the type prefix and the object name, so we need to make sure they start with an uppercase letter and end with a lowercase letter. This way, the scope prefixes stand out from both the type prefix and the object's name. For example, a private integer variable (scope prefix Pi) defined at the module level would be named like this:

  Private nPiCounter As Integer


Variables are named like this:


The name part is simply the variable name written in mixed case, and the type part is a Hungarian type as defined earlier. We don't use type and scope prefixes when naming properties and methods of classes and forms.

The base types and modifiers are given in a table at the end of this appendix. The scope part is defined in the following sections.

Local variables

Local variables do not have a scope prefix. Here are some examples of local variable definitions:

  Dim nCounter  As Integer
  Dim sMessage  As String
  Dim tThisCell As tTableEntry  ' A user-defined type
  ReDim anLookupTable (1 To 10) As Integer

Private variables

Private variables defined at the module level have Pi as a scope prefix. Some examples follow:

  Private nPiCounter  As Integer
  Private sPiMessage  As String
  Private tPiThisCell As tTableEntry  ' A user-defined type
  Private anPiLookupTable () As Integer

Global variables

Public variables defined at the module level of a standard module (that is, a BAS file) have the module identifier as a scope prefix. The module identifier is a unique two-character prefix that suggests the module name. For example, we might choose Er for an error handling module or Db for a database module. We also use an additional scope prefix, pu, to identify the variable as public. Here are some examples.

  Public nPuErCounter As Integer          ' Er for "error handling"
  Public sPuErMessage As String
  Public anPuTbLookupTable () As Integer  ' Tb for "table functions"
  Public tPuTbThisCell As tTableEntry     ' A user-defined type

Form and class properties (unprotected)

Public variables defined at the module level of classes and forms are properties and do not have scope or type prefixes.