Can I hide base class properties in a derived class?

M

Martin Widmer

Hello!

I would like to hide one property of a base class in a derived class. How
could it be done? I tried to override it with a private property, but that
doesn't seem to do the hiding.

Martin
 
C

Cor Ligthert [MVP]

Martin,
I would like to hide one property of a base class in a derived class. How
could it be done? I tried to override it with a private property, but that
doesn't seem to do the hiding.
I assume that you want this for by instance intelligence.

You cannot.

Have a look at the overloaded text property from the picturebox. It does
nothing and still it is there.

Cor
 
M

Martin Widmer

Hi Cor

Cor Ligthert said:
Martin,

I assume that you want this for by instance intelligence.

You cannot.

Have a look at the overloaded text property from the picturebox. It does
nothing and still it is there.

Ok thanks. I will then satisfy myself with overriding it with a dummy
property who doesn't do any harm.

Martin
 
A

Armin Zingler

Martin Widmer said:
Ok thanks. I will then satisfy myself with overriding it with a
dummy property who doesn't do any harm.

You can also throw a 'NotSupportedException'.


Armin
 
H

Herfried K. Wagner [MVP]

Martin Widmer said:
I would like to hide one property of a base class in a derived class. How
could it be done? I tried to override it with a private property, but that
doesn't seem to do the hiding.

Override them with a property which has the same modifier, but specify
'Browsable' and 'EditorBrowsable' attributes on the overriding property.
 
M

Mitchell S. Honnert

Martin, this might not apply to your situation, but what about making the
scope of the base property be Friend? This way, they wouldn't be available
outside the assembly, but could be referenced by derived classes within the
assembly.

For example, in my ID3 tag reading library, I have a base ID3TextFrame which
represents all of the frame types that have a Text property. (An ID3 frame
is just a set of values associated to a particular metadata component, like
Artist or Comments.) This allows me to have common code that decodes these
frame types and sets the Text property, even though in most cases, I don't
want the users of the library to access the Text property directly.

So, the base class property (ID3TextFrame.Text) is a Friend and I expose the
value with a public property in a derived class with a name and data type
that is specific to that frame. For example, ID3TrackNumFrame.TrackNum is a
Public Short property.

Again, this may not apply to you, but it's one way to "hide" a base class
property.

Mitchell S. Honnert
www.UltraID3Lib.com
 
M

Martin Widmer

Hi Mitchell

Mitchell S. Honnert said:
Martin, this might not apply to your situation, but what about making the
scope of the base property be Friend? This way, they wouldn't be
available outside the assembly, but could be referenced by derived classes
within the assembly.
[...]

I have read abotu this approach already, but my class hierarchy is quite
simple and has only 2 levels, and the base class is not abstract, I also use
it. So I cannot declare the property as friend, if I do so, it would not be
available when I instantiate members of the base class, right?

Thanks a lot

Martin
 
M

Martin Widmer

Hi Herfried

[...]
Override them with a property which has the same modifier, but specify
'Browsable' and 'EditorBrowsable' attributes on the overriding property.

Sounds promising... I am not familiar with attributes of properties in
VB.Net. Could you give me a quick example how to do that in the code?

So far I am overriding with a dummy property like this:

Public Overrides Property Placements() As PlacementsCollection
Get
Throw New System.NotSupportedException
Return Nothing
End Get
Set(ByVal value As PlacementsCollection)
Throw New System.NotSupportedException
End Set
End Property


Martin
 
H

Herfried K. Wagner [MVP]

Martin Widmer said:
Sounds promising... I am not familiar with attributes of properties in
VB.Net. Could you give me a quick example how to do that in the code?

\\\
Imports System.ComponentModel
....

<EditorBrowsable(EditorBrowsableState.Never), Browsable(False)> _
Public Overrides Property Placements() As PlacementsCollection
....
///
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top