7.14. Scope of DeclarationsYou have seen declarations of various Visual Basic entities, such as classes, methods, variables and parameters. Declarations introduce names that are used to refer to such entities. A declaration's Scope is the portion of the program that can refer to the declared entity by its name without qualification. Such an entity is said to be "in scope" for that portion of the program. This section introduces several important scope issues. The basic scopes are as follows:
Any block may contain variable declarations. If a local variable or parameter in a method has the same name as a field of the class, the field is "hidden" until the block terminates executionthis is called shadowing. In Chapter 9, we discuss how to access shadowed members. Keep in mind that although a variable may not be in scope, it may still exist. A variable's lifetime is the period during which the variable exists in memory. Some variables exist briefly, some are created and destroyed repeatedly, yet others are maintained through the entire execution of a program. Variables normally exist as long as their container existsfor instance, a local variable of a method will exist as long as the method itself exists in memory. Error-Prevention Tip 7.5
The application in Figs. 7.11 and 7.12 demonstrates scoping issues with fields and local variables. When the application begins execution, module ScopeTest's Main method (Fig. 7.12, lines 47) creates an object of class Scope (line 5) and calls the object's Begin method (line 6) to produce the program's output (shown in Fig. 7.12). Figure 7.11. Scoping rules in a class.
Figure 7.12. Module to the test Scope
In class Scope, line 5 declares and initializes the instance variable x to 1. This instance variable is shadowed (hidden) in any block (or method) that declares a local variable named x. Method Begin (lines 921) declares a local variable x (line 11) and initializes it to 5. This local variable's value is output to show that the instance variable x (whose value is 1) is shadowed in method Begin. The program declares two other methodsUseLocalVariable (lines 2432) and UseInstanceVariable (lines 3541)that each take no arguments and do not return results. Method Begin calls each method twice (lines 1518). Method UseLocalVariable declares local variable x (line 25). When UseLocalVariable is first called (line 15), it creates local variable x and initializes it to 25 (line 25), outputs the value of x (lines 2728), increments x (line 29) and outputs the value of x again (lines 3031). When UseLocalVariable is called a second time (line 17), it recreates local variable x and reinitializes it to 25, so the output of each UseLocalVariable call is identical. Method UseInstanceVariable does not declare any local variables. Therefore, when it refers to x, instance variable x (line 5) of the class is used. When method UseInstanceVariable is first called (line 16), it outputs the value (1) of instance variable x (lines 3637), multiplies the instance variable x by 10 (line 38) and outputs the value (10) of instance variable x again (lines 3940) before returning. The next time method UseInstanceVariable is called (line 18), the instance variable has its modified value, 10, so the method outputs 10, then 100. Finally, in method Begin, the program outputs the value of local variable x again (line 20) to show that none of the method calls modified Begin's local variable x, because the methods all referred to variables named x in other scopes. |