![]() ![]() | ||
What's shadowing all about? It turns out that you can have programming elments in the same module, class, or structure with the same name but different scope. When you have two such elements, and the code refers to the name they share, the compiler uses the element with the narrower, closer scope. This is known as shadowing.
For example, imagine that a module defines a public variable named Data, and a procedure inside the module declares a local variable also named Data. When you use the name Data in the procedure, you use the local variable, but references to Data outside the procedure use the public variable. Here, the procedure-level variable Data shadows the module-level variable Data.
If a derived class redefines an element from a base class, the redefined element shadows the original element. When you access a shadowed element in a derived class, you can do so by qualifying its name with Me. If your derived class shadows the element in the base class, you can access the base class element by qualifying it MyBase. How do you redefine elements this way? You use the Shadows keyword:
Shadows element
Here, element is the name of the class member being shadowed. Here's an example. In this case, I'm declaring a data member in a base class, Pi, that is shadowed by a property of the same name in a derived class—note the use of the Shadows keyword:
Class Class1 Public Pi As Double = 3.1415926535 End Class Class Class2 Inherits Class1 Public Shadows ReadOnly Property Pi() As Double Get Return 3.1415926535 End Get End Property End Class
![]() ![]() | ||