This documentation is archived and is not being maintained.

Variable Declaration

You declare a variable to specify its name and characteristics. The declaration statement for variables is the Dim statement. Its location and contents determine the variable's characteristics.

Declaration Levels

A local variable is one that is declared within a procedure. A module variable is declared at module level, inside the module but not within any procedure internal to that module.

In a class or structure, the category of a nonlocal variable depends on whether or not it is shared. If it is declared with the Shared keyword, it is a shared variable, and it exists in a single copy shared among all instances of the class or structure. Otherwise it is an instance variable, and a separate copy of it is created for each instance of the class or structure. A given copy of an instance variable is available only to the instance for which it was created.

Declaring Data Type

The As clause in the declaration statement allows you to define the data type or object type of the variable you are declaring. You can specify any of the following types for a variable:

  • An elementary data type, such as Boolean, Long, or Decimal
  • A composite data type, such as an array or structure
  • An object type, or class, from Visual Basic or another application, such as Label or TextBox

You can declare several variables in one statement without having to repeat the data type. In the following statements, the variables I, J, and K are declared as type Integer, L and M as Long, and X and Y as Single:

Dim I, J, K As Integer            ' All three are Integer variables.
Dim L, M As Long, X, Y As Single  ' L and M are Long, X and Y are Single.

For more information on data types, see Data Types. For more information on objects, see Object-Oriented Programming in Visual Basic and Programming with Components.

Declaring Lifetime

The lifetime of a variable is the period of time during which it is available for use. A local variable declared with a Dim statement exists only as long as its procedure is executing. When the procedure terminates, all its local variables disappear and their values are lost. However, if you declare a local variable using the Static keyword, it continues to exist and preserve its value even when the procedure ends.

Module, shared, and instance variables retain their values as long as your application continues to run.

For more information on lifetime, see Lifetime.

Declaring Scope

The scope of a variable is the set of all code that can refer to it without qualifying its name. A variable's scope is determined by where the variable is declared. Code located in a given region can use the variables defined in that region without having to qualify their names. When declaring scope, the following rules apply:

  • The scope of a module variable is the entire namespace in which the module is defined.
  • The scope of a shared or instance variable is the structure or class in which it is declared.
  • The scope of a local variable is the procedure in which it is declared.
  • However, if you declare a local variable within a block, its scope is that block only. A block is a set of statements terminated by an End, Else, Loop, or Next statement; for example, a For...Next or If...Then...Else...End If construction.

For more information on scope, see Scope.

Declaring Accessibility

A variable's accessibility is determined by which keyword or keywords — Dim, Public, Protected, Friend, Protected Friend, or Private — you use in the declaration statement. You can declare a module, structure, class, or instance variable with any of these keywords. Within a procedure, only the Dim keyword is allowed, and the accessibility is always private.

For more information on accessibility, see Accessibility.

See Also

Choice of Variable Scope | Data Type Summary | Dim Statement | Public | Protected | Friend | Private | Shared | Static | Declared Element Characteristics