Wednesday 24 June 2009

VB6 Variable Scope

The syntax for declaring a variable in VB is as follows:

[Dim Private Public Static Global] variablename [As datatype]

Note that you use one of five keywords to declare a variable; which one you use depends on the scope you want the variable to have. There are three levels of scope:
· project-level (also called "global" or "application" scope; the variable is accessible to all procedures in all modules of the project)
· module-level (the variable is accessible to all procedures in the module in which it is declared)
· local-level (the variable is accessible only to the procedure in which it is declared)

In addition to the keyword used to declare the variable, the location of the variable declaration also affects its scope. A variable may be declared in one of two places:

· The General Declarations Section of a form or standard module. This section of a module is not labeled as such; it simply exists at the beginning of a code module, after the "Option Explicit" statement but prior to the first Sub or Function procedure. Declaring a variable here makes it a project-level variable (if the Public or Global keyword is used) or a module-level variable (if the Private or Dim keyword is used).

· Within a Sub or Function procedure. Declaring a variable here makes it a local-level variable; only the Dim or Static keyword can be used here. The recommended practice is to declare all local variables within a procedure immediately following the Sub or Function header and prior to any executable statements (although VB will allow you to declare variables anywhere within the procedure).

The following table shows how the five different declarative keywords and the location of their declaration affects the scope:

Keyword Used to Declare the
Variable:
$
Where Declared
à

General Declarations Section of a Form (.frm) Module

General Declarations Section of a Standard (.bas) Module

Sub or Function procedure of a Form or Standard Module
Dim (preferred keyword for local-, but not module-level variables)
module-level scope
module-level scope
local-level scope (value of the variable is NOT preserved between calls
Static
not allowed
not allowed
local-level scope (value of the variable is preserved between calls)
Private (preferred keyword for module-level variables)
module-level scope
module-level scope
not allowed


Public
project-level scope (but references to the variable must be qualified with the form name; also there are some minor restrictions on the types of variables that can be declared as public in a form)

project-level scope

not allowed
Global (the use of this keyword is discouraged; it remains only for compatibility with older versions of VB)

not allowed

project-level scope

not allowed

If the "As datatype" clause is omitted in a variable declaration, the variable type defaults to Variant, unless a type declaration character is used. For example, the following two statements both declare an integer called "intCounter" (the "As" version is preferred):

Dim intCounter As Integer
Dim intCounter%

The following two statements both declare a Variant variable called "vntWhatever":

Dim vntWhatever As Variant
Dim vntWhatever

String variables can be either variable-length or fixed-length. The difference in declaring them is shown below:

'Variable-length string, length changes depending upon length of whatever is assigned to it:
Dim strPlayerName As String

'Fixed-length string, always 2 bytes. Pads or truncates accordingly.
Dim strStateAbbrev As String * 2


Multiple Declarations on One Line

VB allows you to declare any number of variables on one line – but there is a caveat: you must explicitly provide the data type of each variable, even if all the variables are the same data type. For example, you may write the following statement with the intention of declaring three integer variables:

Dim intA, intB, intC As Integer

However, the above statement actually declares intA and intB both as Variant; only intC is Integer. The corrected version of the statement would be:

Dim intA As Integer, intB As Integer, intC As Integer

I recommend declaring only one variable per line.

Initialization of Variables

Unlike many other languages, VB does not allow you to initialize variables; this must be done with an executable statement. However, each variable does have a default initialization value. Numeric variable types are initialized to zero, Strings are initialized to "", Booleans are initialized to False, etc.

No comments:

Post a Comment