Databases Reference
In-Depth Information
is executed, execution switches to
ProcedureB
. While the lines of
ProcedureB
are being
executed, the variable
LocalVar
is out of scope, since it is local to
ProcedureA
. But it is
still valid. In other words, the variable still exists and has a value, but it is simply not
accessible to the code in
ProcedureB
. In fact,
ProcedureB
could also have a local
variable named
LocalVar
, which would have nothing to do with the variable of the same
name in
ProcedureA
.
Once
ProcedureB
has completed, execution continues in
ProcedureA
with the line:
LocalVar = 1
which is a valid instruction, since the variable
LocalVar
is back in scope.
Thus, the lifetime of the local variable
LocalVar
extends from the moment that
ProcedureA
is entered to the moment that it is terminated. This includes the period during
which
ProcedureB
is executed as a result of the call to this procedure, even though during
that period,
LocalVar
is out of scope.
Incidentally, you may notice that the Microsoft help files occasionally get the notions of
scope and visibility mixed up a bit. The creators of the files seem to understand the
difference, but they don't always use the terms correctly.
10.4.12.1 Static variables
To repeat, a variable may go in and out of scope and yet remain valid during that time—
that is, retain a value during that time. However, once the lifetime of a variable expires,
the variable is destroyed, and its value is lost. It is the lifetime that determines the
existence
of a variable; its scope determines its visibility.
Thus, consider the following procedures:
Sub ProcedureA( )
Call ProcedureB
Call ProcedureB
Call ProcedureB
Call ProcedureB
Call ProcedureB
End Sub
Sub ProcedureB( )
Dim x As Integer
x = 5
. . .
End Sub
When
ProcedureA
is executed, it simply calls
ProcedureB
five times. Each time
ProcedureB
is called, the local variable
x
is created anew and destroyed at the end of that
call. Thus,
x
is created and destroyed five times.