469,364 Members | 2,398 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,364 developers. It's quick & easy.

NotInheritable Classes

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
Nov 21 '05 #1
4 4665
Nick,

"Nick Hall" <ni***@aslan.nospam.co.uk> schrieb:
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.


I think the error message is OK, but I would not expect an error message
when I change the modifier to 'Private' ;-).

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2

"Nick Hall" <ni***@aslan.nospam.co.uk> wrote
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.

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?


Keep the NotInheritable, and try:

Shadows ReadOnly Property BaseFlag() As Boolean

That will hide the property in the base class.

You've gotten into a grey area for me as well, be sure to test various
access modes to be sure it will respond as you want....

LFS
Nov 21 '05 #3
Thanks for the suggestion.

I think I will probably just not have the NotInheritable attribute on the
class (in reality it's a nested private class so it shouldn't actually hurt
anything). I was just mildly curious as to whether there was any "good"
reasons why it wouldn't work.

The problem was I wanted to override a base class's interface
implementation, not obscure it so I'm not sure the Shadows suggestion would
work in this scenario.

Heh hum, another strange one to add to the list :-)

Nick Hall

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:eG**************@TK2MSFTNGP15.phx.gbl...

"Nick Hall" <ni***@aslan.nospam.co.uk> wrote
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.

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?


Keep the NotInheritable, and try:

Shadows ReadOnly Property BaseFlag() As Boolean

That will hide the property in the base class.

You've gotten into a grey area for me as well, be sure to test various
access modes to be sure it will respond as you want....

LFS

Nov 21 '05 #4
Comments inline -
"Herfried K. Wagner [MVP]" <hi*******@m.activevb.de> wrote in message
news:ur**************@TK2MSFTNGP12.phx.gbl...
Nick,

"Nick Hall" <ni***@aslan.nospam.co.uk> schrieb:
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.


I think the error message is OK, but I would not expect an error message
when I change the modifier to 'Private' ;-).


I've got to admit I'm a little curious. Why do you think the error message
is OK? And why (if it does make sense) would it not apply to protected
methods as well?

Just trying to understand things a little better :-)

Thanks,

Nick Hall

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>

Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Aguilar, James | last post: by
12 posts views Thread by Langy | last post: by
18 posts views Thread by Edward Diener | last post: by
2 posts views Thread by Siegfried Heintze | last post: by
1 post views Thread by CARIGAR | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.