I've come across what seems to be a compiler problem in the following
scenario. Suppose we have a base class which implements an interface:-
Interface IDemoInterface
ReadOnly Property Flag() As Boolean
End Interface
Public Class BaseClass
Implements IDemoInterface
Protected Overridable ReadOnly Property BaseFlag() As Boolean Implements
IDemoInterface.Flag
Get
Return True
End Get
End Property
End Class
I don't what the property exposed as part of the public interface of the
class and I do want deriving classes to be able to override it if they want.
If I define the deriving class like this: -
Public NotInheritable Class DerivedClass
Inherits BaseClass
Protected Overrides ReadOnly Property BaseFlag() As Boolean
Get
Return False
End Get
End Property
End Class
The compiler shows the following error message: -
'NotInheritable' classes cannot have members declared 'Protected'.
Now I can see that it makes sense to prevent NotInheritable classes defining
Protected methods/properties for the most part. However I would have
thought that as I'm overriding a base class property it should be OK.
Futhermore, overriding Protected methods appears to be allowed by the
compiler, so at the very least it seems inconsistent. Removing the
NotInheritable modifier makes all the errors go away (naturally).
Am I missing something fundamental or is this a bug in the VB compiler?
Regards,
Nick Hall