Shadowing using Shadows in Visual Basic.NET 2005 - Using Shadows with methods in Visual Basic 2005
(Page 4 of 6 )
In previous sections, I discussed both fields and properties using "shadows." "Shadows" are not limited to "Fields" and "Properties;" they can also be used for "methods." Now I'm extending the discussion to "methods."
Consider the following code before we proceed:
Public Class Parent
Public Sub DispMsg()
MessageBox.Show("From Parent")
End Sub
End Class
Public Class Child
Inherits Parent
Public Shadows Sub DispMsg()
MessageBox.Show("From child")
End Sub
End Class
In the above two classes, there exists the same method, "DispMsg," with the same signature (which may have different implementations). Thus we need "shadows."
We can test the above classes using the following style of code:
Dim objParent As New Parent
Dim objChild As New Child
objParent.DispMsg()
objChild.DispMsg()
And finally, using a parent reference, we can test as follows (which displays "From Parent"):
Dim RefParent As Parent = objChild
RefParent.DispMsg()
We can even access a parent member without creating a separate reference variable as follows:
CType(objChild, Parent).DispMsg()
The above applies even to shadowed "fields" and "properties."
Next: Using Shadows with overloaded parent methods in Visual Basic 2005 >>
More Visual Basic.NET Articles
More By Jagadish Chaterjee