views:

1015

answers:

3

What is the difference between a class with protected constructors and a class marked as MustInherit? (I'm programming in VB.Net but it probably equally applies to c#).

The reason I ask is because I have an abstract class that I want to convert the constructors to shared/static methods. (To add some constraints).

I can't do this because it's not possible to create an instance in the shared function.

I'm thinking to just remove the MustInherit keyword. Will this make any difference?

Thanks.

ETA:

I think i've answered my question, If I remove the MustInherit keyword, I can no longer include the MustOverrides, which are very useful.

With that in mind, is there any way around my problem?

ETA2:

To clarify, I can't do the below unless I remove the MustInherit keyword?

Public MustInherit MyBaseClass

Private Sub New() 
End Sub 

Protected Function CreateInstance(ParmList) As MyBaseClass

 If ParmList is Ok Then Return New MyBaseClass()

End Function

End Class

+1  A: 

You can call the Protected constructor using reflection and instantiate the class but you can't instantiate an abstract class in this way. You can declare MustOverride methods in MustInherit classes but Protected constructor can enforce nothing on derived classes.

You should always declare classes that are conceptually abstract as MustInherit. Protected constructors can be useful when you are providing it along with some Public overloads to provide some more functionality to derived classes.

Mehrdad Afshari
Yes, as usual,I realised as soon as after posting that I needed an abstract class in order to include the MustOverride methods.So, to clarify, I can't do the below unless I remove the MustInherit keyword?Public MustInherit MyBaseClass Private Sub New() End Sub Protected Function CreateInstance(ParmList) As MyBaseClass If ParmList is Ok Then Return New MyBaseClass() End FunctionEnd Class
Jules
That's a mess, I've added the above to the OP!
Jules
Jules: Yes, your understanding is correct.
Mehrdad Afshari
A: 

If the class only has a protected constructor, it is still possible to have an instance of the class which can stand on its own. It would require working around the protected constructor, such as using reflection.

If the class is marked as MustInherit, it is impossible to have an instance of that class on its own. Instances can only be created of the derived/inherited classes.

g .
A: 

Not really sure what you want.

If you need to create an object of the abstract class, I recommend you create a private class implementation of your abstract class and return it in your CreateInstanceMethod:

Public MustInherit MyBaseClass
    Private BaseClassImplementation
        Inherits MyBaseClass

        ...
    End Class

    Public Function CreateInstance(paramList) as MyBaseClass
        If paramList Is Ok Then Return New BaseClassImplementation
    End Function
End Class

However, if you want to add some constraints to the construction, I recommend to throw exceptions:

Public MustInherit MyBaseClass
    Protected Sub New(paramList)
        If paramList IsNot Ok Then Thow New Exception
        ...
    End Sub
End Class
Wilhelm