Re: Why Overloads needed

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Lee Silver (LSilver_at_information-concepts.com)
Date: 06/05/04


Date: Sat, 05 Jun 2004 14:07:43 -0400

Jay:

Aha, I think I got it -- the key phrase is "Shadows is used for versioning".
Without Overloads, Shadows would be required; and since neither is implied I
need to specify Overloads.

But this brings up something interesting... In my Base class I have a
non-overloaded overridable property, Item. In my derived class I declare it
Overrides, without Overloads or Shadows, and it compiles just fine. If at some
point in the future I modify Base class to overload Item, won't that break my
derived class?

-- 
// Lee Silver
// Information Concepts Inc.
// http://www.information-concepts.com
Facilitating the automated conversion of Data into Information
since 1982
> Lee,
> In addition to the other comments:
> 
> Overloads is needed so the compiler will know if Sub Remove(Object) will be
> available via your derived class!
> 
> Consider the following:
> 
>     Option Strict On
> 
>     Public Class Base
> 
>         Public Overridable Sub Remove(ByVal wIndex As Integer)
> 
>         End Sub
> 
>         Public Overridable Sub Remove(ByVal wValue As Object)
> 
>         End Sub
> 
>         Public Sub Main()
>             Dim overlord As New OverloadedBase
>             overlord.Remove(10)
>             overlord.Remove("Jay")
> 
>             Dim underlord As New ShadowsBase
>             underlord.Remove(10)
>             underlord.Remove("Jay")
>         End Sub
> 
>     End Class
> 
>     Public Class OverloadedBase
>         Inherits Base
> 
>         Public Overloads Overrides Sub Remove(ByVal wIndex As Integer)
>         End Sub
> 
>     End Class
> 
> 
>     Public Class ShadowsBase
>         Inherits Base
> 
>         Public Overrides Sub Remove(ByVal wIndex As Integer)
>         End Sub
> 
>     End Class
> 
> 
> The underload.Remove("Jay") method has an error, because you forgot to
> include Overloads, so Shadows was implicitly used. Shadows causes
> Base.Remove(Object) to be hidden in OverloadedBase set of methods. Without
> the Option Strict On, VB.NET will attempt to convert "jay" to an integer at
> run time on the underload.Remove("Jay") line.
> 
> Shadows is used for versioning, so methods added to version 2 of classes
> don't suddenly cause surprises in classes that were designed for version 1
> of the base class.
> 
> NOTE: Shadows base can be written as:
> 
>     Public Class ShadowsBase
>         Inherits Base
> 
>         Public Shadows Overrides Sub Remove(ByVal wIndex As Integer)
>         End Sub
> 
>     End Class
> 
> Which will remove the warning about adding either Shadows or Overloads.
> 
> Hope this helps
> Jay
> 


Relevant Pages

  • Re: Why Overloads needed
    ... You example is largely the reason VB.NET defaults to Shadows. ... Your derived class still ... > need to specify Overloads. ... > point in the future I modify Base class to overload Item, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Why Overloads needed
    ... Overloads is needed so the compiler will know if Sub Removewill be ... so Shadows was implicitly used. ... > In a base class I have the following 2 declarations: ... > In an immediately derived class I have the following declaration: ...
    (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: Overloads/Shadows question
    ... I find Shadows to be best used for versioning control. ... Public Shadows Sub Show ... the other overloads? ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Inheritance design issue
    ... > It seems to me that this would be a common situation faces in OOP, ... Indicates that this property shadows an identically named ... programming element, or set of overloaded elements, in a base class. ... Mybase) or provide a property to access the bass class property (i.e. ...
    (microsoft.public.dotnet.languages.vb)