Como: Ocultar uma variável herdada (Visual Basic)
A derived class inherits all the definitions of its base class. If you want to define a variable using the same name as an element of the base class, you can hide, or shadow, that base class element when you define your variable in the derived class. If you do this, code in the derived class accesses your variable unless it explicitly bypasses the shadowing mechanism.
Another reason you might want to hide an inherited variable is to protect against base class revision. The base class might undergo a change that alters the element you are inheriting. If this happens, the Shadows modifier forces references from the derived class to be resolved to your variable, instead of to the base class element.
To hide an inherited variable
Be sure the variable you want to hide is declared at class level (outside any procedure). Otherwise you do not need to hide it.
Inside your derived class, write a Instrução Dim (Visual Basic) declaring your variable. Use the same name as that of the inherited variable.
Include the Shadows (Visual Basic) keyword in the declaration.
When code in the derived class refers to the variable name, the compiler resolves the reference to your variable.
The following example illustrates shadowing of an inherited variable.
Public Class shadowBaseClass Public shadowString As String = "This is the base class string." End Class Public Class shadowDerivedClass Inherits shadowBaseClass Public Shadows shadowString As String = "This is the derived class string." Public Sub showStrings() Dim s As String = "Unqualified shadowString: " & shadowString & vbCrLf & "MyBase.shadowString: " & MyBase.shadowString MsgBox(s) End Sub End Class
The preceding example declares the variable shadowString in the base class and shadows it in the derived class. The procedure showStrings in the derived class displays the shadowing version of the string when the name shadowString is not qualified. It then displays the shadowed version when shadowString is qualified with the MyBase keyword.
Programação robusta
Shadowing introduces more than one version of a variable with the same name. When a code statement refers to the variable name, the version to which the compiler resolves the reference depends on factors such as the location of the code statement and the presence of a qualifying string. This can increase the risk of referring to an unintended version of a shadowed variable. You can lower that risk by fully qualifying all references to a shadowed variable.
Consulte também
Tarefas
Como: Ocultar uma variável com o mesmo nome que sua variável (Visual Basic)
Como: Acessar uma variável ocultada por uma classe derivada (Visual Basic)
Referência
Conceitos
Diferenças entre sombreamento e sobreposição (Visual Basic)
Me, My, MyBase e MyClass no Visual Basic
Noções básicas de herança (Visual Basic)