hide existing property

  • Thread starter Thread starter --== Alain ==--
  • Start date Start date
A

--== Alain ==--

Hi,

I derived a control to create my custom control.
However, some of the control base properties are useless from my point
of view and i would like to avoid control user to change them.

How can i do that ?

I need to hide them from Property window editor but also to make them
not reachable via code line.

thanks a lot,

Al.
 
--== Alain ==-- said:
I derived a control to create my custom control.
However, some of the control base properties are useless from my point
of view and i would like to avoid control user to change them.

[...]

The idea of inheritance is to *add* functionality to a class, not take
it away. That said, there really is no way to accomplish your desired goal.

Hope that helps,
 
Alain,

You can hide them from the proeprty window by applying Browsable(false)
attribute to them. You can also hide them from IntelliSense by applying
EditorBrowsable(EditorBrowsableState.Never) attribute. You cannot remove
them from the class though. This would be against object oriented
principles.

To apply those attributes you need to override or hide the base class
properties. In case of overriding (virtual properties) you can implement
your new version to throw exception if the user tries to set values. With
non-virtual properties there is not complete solution. The new version that
hides the base property can throw exception, but if the user uses reference
to the object that is of type the base class nothing could stop her/him of
setting new values.
 
I derived a control to create my custom control.
However, some of the control base properties are useless from my point of
view and i would like to avoid control user to change them.

How can i do that ?

I need to hide them from Property window editor but also to make them not
reachable via code line.

thanks a lot,

In addition to what Stoitcho already said, there are other measues you can
take such as changing the property's access specifier in your derived class,
implementing "ICustomTypeDescriptor" (or using its new 2.0 cousin
"TypeDescriptionProviderAttribute)", etc (where you can completely redefine
the properties that will be accessible in a property grid). However, this is
not a panacea and will only complicate your life. My personal opinion is to
simply create a new control that wraps the old one instead of inheriting
from it. You can then delegate to the old control as needed and expose only
those properties you want. Note BTW that you usually don't need to expose
all members of the original control at once. I personally only expose those
methods/properties I actually need and add to it on an as-needed basis.
 
--== Alain ==-- said:
I derived a control to create my custom control.
However, some of the control base properties are useless from my point
of view and i would like to avoid control user to change them.

How can i do that ?

I need to hide them from Property window editor but also to make them
not reachable via code line.

That would break Liskov's Substitutability Principle. If you don't want
to inherit members from a class, don't derive from that class.
 
Why not create a facade for this class. You can make a private field for the
class you want to derive from. So don't inherit from it but make it private
available in your facade class through compisition.

HTH

Bart
 
available in your facade class through compisition.Sorry typo:

That should be composition of course :)

Bart
 

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

Back
Top