Shadowing using Shadows in Visual Basic.NET 2005 - Using Shadows with properties in Visual Basic 2005
(Page 3 of 6 )
In previous sections, we concentrated only on fields. Now I shall extend the same discussion to "properties" with "shadows" in this section.
Let us consider the following classes:
Public Class Parent
Private _x As Integer
Public Property X() As Integer
Get
Return _x
End Get
Set(ByVal value As Integer)
_x = value
End Set
End Property
End Class
Public Class Child
Inherits Parent
Private _x As Integer
Public Shadows Property X() As Integer
Get
Return _x
End Get
Set(ByVal value As Integer)
_x = value
End Set
End Property
End Class
In the above classes, I declared the same two members in each (one field and one property). You should clearly observe that the field "_x" is not defined with "shadows" even though it is available in both. Neither has it given a warning. This is because the field is mentioned with "private." When a member is defined as "private," it is invisible outside the class and thus doesn't require any "overrides" or "shadows."
Unlike the field "_x," the property "X" is defined as "public" and is identically declared in both; thus it requires "shadows."
We can test the above classes using the following style of code:
Dim objParent As New Parent
objParent.X = 10
Dim objChild As New Child
objChild.X = 20
MessageBox.Show("From parent: " & objParent.X)
MessageBox.Show("From child: " & objChild.X)
It displays 10 and 20 as expected. We can even modify the value of the parent member from the child instance as shown below:
Dim RefParent As Parent = objChild
RefParent.X = 30
MessageBox.Show("From parent: " & objParent.X)
MessageBox.Show("From child: " & objChild.X)
MessageBox.Show("From child with parent ref: " & RefParent.X)
And now, it displays 30, 20 and 30.
Next: Using Shadows with methods in Visual Basic 2005 >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee