Section 9.3. Class Scope


9.3. Class Scope

In Section 7.16, we discussed method scope; now, we consider class scope. A class's instance variables and methods belong to its scope. Within a class's scope, class members are accessible to all of the class's methods and properties and can be referenced simply by name (without an object reference). Outside a class's scope, class members cannot be referenced directly by name. Those class members that are visible (such as Public members) can be accessed through a "handle" (i.e., members can be referenced via names of the form objectReferenceName.memberName or ClassName.memberName for Shared members). For example, line 56 of Fig. 9.2 accessess a Time object's method with the method call time.ToUniversalString().

If a variable is defined in a method, only that method can access the variable (i.e., the variable is a local variable of that method). Such variables have block scope. If a method defines a local variable that has the same name as an instance variable, the local variable hides the instance variable in that method's scopethis is called shadowing. A shadowed instance variable can be accessed in a method of that class by preceding its name with the keyword Me and the dot separator, as in Me.hourValue. Section 9.8 discusses keyword Me.



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