473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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 4813
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
9
by: Jack | last post by:
Hello I have a library of calculationally intensive classes that is used both by a GUI based authoring application and by a simpler non-interactive rendering application. Both of these...
9
by: Aguilar, James | last post by:
I know that one can define an essentially unlimited number of classes in a file. And one can declare just as many in a header file. However, the question I have is, should I? Suppose that, to...
12
by: Langy | last post by:
Hello I'm fairly new to C++ but have programmed several other languages and found most of c++ fairly easy (so far!). I've come to a tutorial on classes, could someone please tell me why you...
2
by: joye | last post by:
Hello, My question is how to use C# to call the existing libraries containing unmanaged C++ classes directly, but not use C# or managed C++ wrappers unmanaged C++ classes? Does anyone know how...
18
by: Edward Diener | last post by:
Is the packing alignment of __nogc classes stored as part of the assembly ? I think it must as the compiler, when referencing the assembly, could not know how the original data is packed otherwise....
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
0
by: ivan.leben | last post by:
I am writing this in a new thread to alert that I found a solution to the problem mentioned here: http://groups.google.com/group/comp.lang.c++/browse_thread/thread/7970afaa089fd5b8 and to avoid...
2
by: Siegfried Heintze | last post by:
Both C# and VB.NET have some strange keywords. When would someone want to use NotInheritable (or sealed in C#)? When would someone want to use shadows? Thanks, Siegfried
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.