By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,651 Members | 1,676 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,651 IT Pros & Developers. It's quick & easy.

Overloads vs Shadows

P: n/a
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.

Question 1 - Which is preferred in a case like this, Shadows or Overloads.

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?

-------------------------

Public Class TableLookup(Of TKey, TValue)
Inherits System.Collections.Generic.Dictionary(Of TKey, TValue)

Default Public Shadows Property Item(ByVal Key As TKey) As TValue
Get
If Not ContainsKey(Key) Then Return Nothing Else Return
MyBase.Item(Key)
End Get
Set(ByVal value As TValue)
If value Is Nothing Then MyBase.Remove(Key) Else MyBase.Item(Key) =
value
End Set
End Property

End Class

Oct 24 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Oct 24 '07 #2

P: n/a
Thanks for the response. I was leaning towards shadows after doing some
reading - your response filled in gaps in my understanding.

"Göran Andersson" wrote:
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
Oct 24 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.