Shadowing using Shadows in Visual Basic.NET 2005 - Using Shadows with overloaded parent methods in Visual Basic 2005
(Page 5 of 6 )
This is a different situation when compared to any of the previous sections. I would like to have overloaded methods in the parent and "shadow" a method in the child.
Let us go through the following code first:
Public Class Parent
Public Sub DispMsg()
MessageBox.Show("From Parent")
End Sub
Public Sub DispMsg(ByVal Msg As String)
MessageBox.Show(Msg)
End Sub
End Class
Public Class Child
Inherits Parent
Public Shadows Sub DispMsg()
MessageBox.Show("From child")
End Sub
End Class
In the "Parent" class I defined two overloaded methods named "DispMsg" (one with a parameter and the other without a parameter). In the "Child" class I am shadowing only the method that does not have a parameter.
Let us try to access the members with the following testing statements:
Dim objParent As New Parent
objParent.DispMsg()
objParent.DispMsg("Custom msg for parent")
No problem, the above works as the parent has both methods. Let us proceed to the following:
Dim objChild As New Child
Dim RefParent As Parent = objChild
objChild.DispMsg()
'objChild.DispMsg("Custom msg for child") 'wont work with shadow
RefParent.DispMsg()
RefParent.DispMsg("Custom msg from ref object")
From the above, you can understand that we can still access both parent methods from a parent reference. But the child can access only the shadowed method. In other words, the keyword "shadows" completely hides based on name and not on signature.
Next: Using Overloads instead of Shadows with overloaded parent methods in Visual Basic 2005 >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee