Why no Shared MustOverride methods?

DavidB

New member
Joined
Jan 28, 2007
Messages
1
Programming Experience
10+
I apologize if this has been asked and answered already, but am trying to understand why I can't declare a method in a base class Shared MustOverride, so that my derived classes are required to implement their own specific implementation of the shared method. I would be happy to implement the base "object" as either a Class or an Interface. Is this contradictory to object-oriented design philosophies, or just something MS has chosen not to implement?

As a very simple example, I would like to have the following:


Public Mustinherit Class ThingFormatter
Public MustOverride SharedFunction GetFormattedData(ByVal theObject As ThingToFormat) As String
End Class


Public Class TypeAFormatter

Inherits ThingFormatter
Public Overrides Shared Function GetFormattedData(ByVal theObject As ThingToFormat) As String
return theObject.SomeMethodThatReturnsString().ToLower()

End Function

End Class

Any help, or direction to appropriate resources would be greatly appreciated.
 
If the method is Shared there is nothing tying it to neither Base class or derived classes, inheritance and instances does not apply for Shared members. For example the shared member in your Mustinherit ThingFormatter can be accessed with the instance-less call ThingFormatter.GetFormattedData here.

You can declare a shared member in Base class, but will not be able to access it from instance-less derived class without instance, and then you get a compiler warning.

Derived class can still define its Shared OverLoads. If you overload by same signature you in effect hide the base implementation.

If the method depends on its class instance it cannot be declared Shared.
 
Back
Top