Overloads vs Shadows

G

Guest

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
 
G

Guest

AMercer said:
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

Guest

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top