Hiding inherited properties

  • Thread starter Thread starter Steven Nagy
  • Start date Start date
S

Steven Nagy

Hi all,

How would I go about hiding an inherited property?
In particular, I want to hide the 'BackColor' property of the
UserControl class.
I will then implement my own back color related properties and
collections.
I can do all the latter, but I don't want to build this nice designable
properties and have this big ugly 'BackColor' property available to the
user of my components, which will need to be set to Transparent all the
time for my other stuff to work. I just want to hide it, make it
private or something.

I really don't want to have to build something from scratch, but IF I
did, I would start by creating a class and implementing IComponent
right?

Many thanks,
Steven Nagy
 
Well, you can't totally hide it, but you can do this:

private new Color BackColor
{
get { return base.BackColor; }
set { base.BackColor = value; }
}

The "new" will cause your newly declared "BackColor" property to "hide"
the base class's property, and since your new property is private,
nobody can see it or call it.

That said, callers can still get at the base class's BackColor property
like this:

MyFancyUserControl mine = new MyFancyUserControl();
UserControl castToBase = mine;
castToBase.BackColor = Color.Brown;

By casting to the base class UserControl, it is the base class's
BackColor property that becomes visible and will be invoked.

Now, the Designer's property grid is another question. I don't know
whether it will respect the "new" private property or not... but it's
worth a try.
 

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