storage class of a
variable determines which parts of the program can access it and how long it stays in
- The time period between the creation and destruction of a variable is called its lifetime
(or sometimes its duration).
- Some variables exist briefly, some are repeatedly created and destroyed, and others
exist for the entire program execution.
- Visual Basic provides two storage class specifiers: automatic and Static.
- A variable's visibility describes where within a program it can be
- The word scope is also used to describe visibility. The scope of a
variable is that part of the program where the variable is visible, in other words, the
region of a project in which that identifier can be referenced.
- Some identifiers can be referenced throughout a program, while others can be referenced
from only limited portions of a program.
- For example, when we declare a local variable in a procedure, it can be referenced only
in that procedure.
Automatic (local) variables
- Variables defined within a procedure body are called automatic variables or
- Local variables are known only in the procedure in which they are defined.
- Local variables have automatic storage class by default and are created when the
procedure becomes active. Until then, there is no place in memory where their values are
stored; they are undefined.
- When the procedure exits and control is returned to the calling program, the variables
are destroyed and their values are lost.
- The name automatic is used because the variables are automatically created when a
procedure is called and automatically destroyed when it returns.
- The lifetime of an automatic variable coincides with the time when the procedure in
which it is defined is executing.
- Automatic variables can only be accessed within the function in which they are
- Limited visibility insures that the variables in one function are safe from accidental
alteration by other functions because the other functions can't see them.
- Keyword Static is used to declare identifiers for variables and procedures of the
Static storage class.
- For Static variables, storage is allocated and initialized once when the form is
- Local variables declared with keyword Static are still known only in the
procedure in which they are defined, but unlike automatic variables,
Static variables retain their value when the procedure is exited. The next time the
procedure is called, the Static local variable contains the value it had when the
procedure last exited.
- All numeric variables of the Static storage class are initialized to zero.
- The following statement declares local variable count to be Static (and
implicitly initializes count to 0):
Static count As Integer
If a static variable is initialized in its declaration, it will not be
reinitialized in subsequent calls to the procedure.
Static count As Integer = 1
Three scopes for an identifier are:
- Local scope
(or block scope) applies to variables declared in a procedure's
body. Local variables can be referenced from the point at which they are declared through End
Sub (or End Function).
- Module scope applies to variables declared in the general
declaration with Dim. By default, module variables can only be referenced in the
module in which they are declared. Module scope is sometimes called Private scope.
- Public scope
(or namespace scope) refers to module variables that are declared Public.
Module variables that are Public are accessible to all modules.
When an identifier in an outer scope (such as module scope) has the same name as an
identifier in an inner scope (such as local scope), the identifier in the outer scope is
"hidden" until the inner scope terminates. This means that while executing in
the inner scope, the inner scope sees the value of its own identifier and not the value of
the identically named identifier in the outer scope.
Local variables declared Static still have local scope even though they exist from the
time the form is loaded into memory. Thus, storage duration does not affect the scope of