Re: Overloads vs Shadows
- From: Göran Andersson <guffa@xxxxxxxxx>
- Date: Wed, 24 Oct 2007 17:43:02 +0200
AMercer wrote:
In the class below, I inherit from Generic.Dictionary so I can override property Item. Item is not overridable, so I used Shadows, and it works as I want. It works equally well if I replace Shadows with Overloads.
Yes, the Overloads keyword can also be used for shadowing.
Question 1 - Which is preferred in a case like this, Shadows or Overloads.
The Shadows keyword is more descriptive, as that is what you are doing. You should use the Overloads keyword for shadowing only when you are also doing overloading, in which case you can't use the Shadows keyword.
Question 2 - The Override clan (Overrides, Overridable, NotOverridable, MustOverride) seems to have lost some steam if I can effect an override on something that is not declared as overridable. In other words, the totality of what I can say with these modifiers seems to be redundant and/or inconsistent. Or maybe not - am I paying a price that I don't know about by using Shadows or Overloads in this way?
Shadowing and overriding are two completely different things. An overridable method is virtual, a shadowed method is not.
Example:
Public Class Test1
Public Sub Show()
Console.WriteLine("Test1")
End Sub
End Class
Public Class Test2
Inherits Test1
Public Shadows Sub Show()
Console.WriteLine("Test2")
End Sub
End Class
Dim test As Test1 = New Test2
test.Show()
Output:
Test1
:: For a shadowed method, the type of the reference decides what method is used.
Public Class Test3
Public Overridable Sub Show()
Console.WriteLine("Test3")
End Sub
End Class
Public Class Test4
Inherits Test3
Public Overrides Sub Show()
Console.WriteLine("Test4")
End Sub
End Class
Dim test As Test3 = New Test4
test.Show()
Output:
Test4
:: For an overridden method, the actual type of the object decides what method is used.
--
Göran Andersson
_____
http://www.guffa.com
.
- Follow-Ups:
- Re: Overloads vs Shadows
- From: AMercer
- Re: Overloads vs Shadows
- Prev by Date: Print Manager
- Next by Date: Re: how to impose numerical limits on a textbox??
- Previous by thread: Print Manager
- Next by thread: Re: Overloads vs Shadows
- Index(es):
Relevant Pages
|
Loading