Section 7.14. Scope of Declarations


7.14. Scope of Declarations

You 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:

  1. Block scope The scope of a variable declared in a block is from the point of the declaration to the end of the block (e.g., a variable declared in a control statement's body is in scope only until the end of that control statement).

  2. Method scope The scope of a method's local-variable declaration or parameter is from the point at which the declaration appears to the end of that method. This is similar to block scope.

  3. Module scope The scope of a class's members or a module's members is the entire body of the class or module. This enables non-Shared methods of a class to use all of the class's members. (Note that Shared methods of a class can access only the class's Shared variables and Shared methods.) All module members are implicitly Shared, so all methods of a module can access all variables and other methods of the module.

  4. Namespace scope Elements declared in a namespace (i.e., classes, modules, interfaces, delegates, enumerations, structures and other namespaces) are accessible to all other elements in the same namespace. By default, all elements of a project are part of a namespace that uses the project's name. This enables, for example, a module method in the project to create an object of a class that is also part of the project.

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

Use different names for fields and local variables to help prevent subtle logic errors that occur when a method is called and a local variable of the method shadows a field of the same name in the class.


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.

  1  ' Fig. 7.11: Scope.vb  2  ' Scope class demonstrates instance and local variable scopes.  3  Public Class Scope  4     ' instance variable that is accessible to all methods of this class  5     Private x As Integer = 1                                             6  7     ' method Begin creates and initializes local variable x and  8     ' calls methods UseLocalVariable and UseInstanceVariable  9     Public Sub Begin() 10        ' method's local variable x shadows instance variable x 11        Dim x As Integer = 5                                    12 13        Console.WriteLine("local x in method Begin is " & x) 14 15        UseLocalVariable() ' UseLocalVariable has local x 16        UseInstanceVariable() ' uses class Scope's instance variable x 17        UseLocalVariable() ' UseLocalVariable reinitializes local x 18        UseInstanceVariable() ' Scope's instance variable x retains value 19 20        Console.WriteLine(vbCrLf & "local x in method Begin is " & x) 21     End Sub ' Begin 22 23     ' create and initialize local variable x during each  call 24     Sub UseLocalVariable() 25        Dim x As Integer = 25 ' initialized in each call 26 27        Console.WriteLine( _ 28           vbCrLf & "local x on entering method UseLocalVariable is " & x) 29        x += 1 ' modifies this method's local variable x 30        Console.WriteLine("local variable x before exiting method " & _ 31           "UseLocalVariable is " & x) 32     End Sub ' UseLocalVariable 33 34     ' modify class Scope's instance variable x during  each call 35     Sub UseInstanceVariable() 36        Console.WriteLine(vbCrLf & "instance variable"  & _ 37           " x on entering method UseInstanceVariable is " & x) 38        x *= 10 ' modifies class Scope's instance variable x 39        Console.WriteLine("instance variable " & _ 40           "x before exiting method UseInstanceVariable is " & x) 41     End Sub ' UseInstanceVariable 42  End Class ' Scope 

Figure 7.12. Module to the test Scope

 1  ' Fig. 7.12: ScopeTest.vb 2  ' Testing class Scope. 3  Module ScopeTest 4     Sub Main() 5        Dim testScope As New Scope() 6        testScope.Begin() 7     End Sub ' Main 8  End Module ' ScopeTest 

[View full width]

local x in method Begin is 5 local x on entering method UseLocalVariable is 25 local variable x before exiting method UseLocalVariable is 26 instance variable x on entering method UseInstanceVariable is 1 instance variable x before exiting method UseInstanceVariable is 10 local x on entering method UseLocalVariable is 25 local variable x before exiting method UseLocalVariable is 26 instance variable x on entering method UseInstanceVariable is 10 instance variable x before exiting method UseInstanceVariable is 100 local x in method Begin is 5



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.



Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net