< Day Day Up > |
Scope determines which procedures can access a variable or procedure. A variable's lifetime determines when and for how long that variable is live (active in memory) and can therefore be accessed. To help clarify lifetime, within the context of a variable, here are a few guidelines that will help:
When a procedure is executed, VBA initializes all the variables within that procedure's scope. That means each variable has a value, as listed in Table 9.1, even before the code assigns one. These initial values remain intact until the code explicitly assigns a value to the variable.
Now, let's look at the relationship between a variable's scope and its lifetime:
The Lifetime of a Procedure-Level VariableYou've already experienced a procedure-level variable's lifetime it's the same as the procedure's scope. When you execute the procedure, VBA initializes the variables. When the procedure quits, the variables lose their value. At procedure level, the scope is limited to the procedure itself and the lifetime lasts only as long as the procedure is running. The following procedure shows a variable that's changing values throughout the lifetime of the procedure (see Figure 9.6): Function ProcedureLT1() Dim strPLT As String strPLT = "Variable is alive" Debug.Print strPLT strPLT = strPLT & " and well" Debug.Print strPLT End Function Figure 9.6. Procedure-level variables maintain a value as long as the procedure is running.This example defines and then redefines the strPLT variable. Notice that the variable doesn't disappear after it's used. You don't have to redeclare it each time you define it or modify it. In fact, you can redefine the variable as many times as you like. It's available as long as the code's running. The Lifetime of a Module-Level VariableNow let's take the next step and examine the lifetime of a module-level variable. Let's use the existing procedures in the Chapter 9 sample module as follows:
If you try to access intMTL from another module, VBA will not see it and will return a runtime error. However, the variable is still alive and useable within the scope of its own module. Don't confuse this behavior with the variable's lifetime. This behavior is a scoping issue and not a matter of the variable's life or death. The Lifetime of a Public VariablePublic variables are accessible by any procedure. After they are initialized, they live until destroyed. You can change the variable's value, but the variable itself still returns a value. To illustrate the long life of a public variable, change the declaration for intMTL in the General Declarations section of your Chapter 9 module to a Public declaration. Then, enter the following procedure in your second Chapter 9 sample module (the one with just PublicVariableTest): Function PublicLTTest() Debug.Print intMLT End Function Figure 9.8 shows the result of executing PublicLTTest. Because the variable is public, PublicLTTest can refer to it from your second module. In addition, the variable's value remains intact. A module-level variable and a public variable can have the same lifetime, but not share the same scope. That means a variable might not be available to all procedures in the application, even though it is residing alive and well in memory. Figure 9.8. A public variable lasts as long as the code is running. |
< Day Day Up > |