By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,165 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

NotInheritable Classes

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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.