Re: Overloads vs Shadows



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
.



Relevant Pages

  • Re: Compiler confusion with using Shadows keyword
    ... I think Shadows was somehow meant to allow you to override what the designer ... I have no idea where Shadowing fits into the design anyway. ... > Public Class ShadowsMethod ...
    (microsoft.public.dotnet.languages.vb)
  • Re: overides/loads usage
    ... your derived member to override the base class member. ... difference bvetween Shadows and Overrides. ... Overridable Sub Step1 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Overriding properties and events
    ... >> Only if the base class has that item marked as overrideable. ... >> you'll need to use Shadows. ... You should be able to override them for *virtual* events. ...
    (microsoft.public.dotnet.general)
  • Re: Interface suggestions
    ... the original & override the value. ... Public Shadows Property FlatStyle() As FlatStyle ... The reason we need the DefaultValue attribute on it to let the Designer know ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Overloads vs Shadows
    ... Item is not overridable, so I used Shadows, and it works as I ... MustOverride) seems to have lost some steam if I can effect an override on ... Public Class Test1 ... Public Sub Show ...
    (microsoft.public.dotnet.languages.vb)

Loading