I've recently been developing a custom control (BarControl) that derived from an existing control (FooControl). There were two requirements related to the derived control's border style:
BarControl
FooControl
BorderStyle
Since the base control exposed a BorderStyle property, I thought I'd need to do three things:
protected
[Browsable(false)]
So I blithely whipped out the following code:
[Browsable(false)] protected override BorderStyle BorderStyle { get { return base.BorderStyle; } set { base.BorderStyle = value; } }
The compiler just as blithely hit me with the following error:
Error 1 'BarControl.BorderStyle.get': cannot override inherited member 'FooControl.BorderStyle.get' because it is not marked virtual, abstract, or override Error 2 'BarControl.BorderStyle.set': cannot override inherited member 'FooControl.BorderStyle.set' because it is not marked virtual, abstract, or override
Oops! I had expected the BorderStyle property to be virtual and it wasn't. The solution--like most solutions--was obvious once I found it: use the new, rather than the override, modifier to hide the base class property. So I quickly changed the code to:
new
override
[Browsable(false)] protected new BorderStyle BorderStyle { get { return base.BorderStyle; } set { base.BorderStyle = value; } }
Now the compiler was happy, so everything would work as expected, right? I switched to my test app, drug an instance of my control onto a form, and looked for the BorderStyle property in the Properties pane. Lo and behold, it was still there. I was stumped. I had hidden the base property and marked the new property with [Browsable(false)]. What could be wrong?
It took a close reading of the documentation for the new modifier to find the problem: the new modifier overrides members with the same signature. Since the access modifier is part of a member's signature, changing the access modifier from protected to public meant I wasn't hiding the BorderStyle property in the base class. As a result, the BrowsableAttribute was applied to the BorderStyle property in the derived class but not the BorderStyle property in the base class.
public
BrowsableAttribute
What to do. I needed to hide the BorderStyle property at design time, so I needed to make it public in the derived class; however, I also needed to hide the property at runtime so I needed to make it protected. Perhaps, I could apply the protected access modifier to the property's getter and setter methods while still leaving the property access modifier as public. So I tried this:
[Browsable(false)] public new BorderStyle BorderStyle { protected get { return base.BorderStyle; } protected set { base.BorderStyle = value; } }
The compiler didn't like it:
Error 1 Cannot specify accessibility modifiers for both accessors of the property or indexer 'BarControl.BorderStyle'
Since the goal was to keep the property from being modified at runtime, I settled on the following solution:
[Browsable(false)] public new BorderStyle BorderStyle { get { return base.BorderStyle; } protected set { base.BorderStyle = value; } }
I'm not sure that there isn't a better solution, but this meets the criteria:
So the three things I actually needed to do were
I hope you find this helpful.
CheersDan
Disclaimer: This posting is provided "AS IS" with no warranties, and confers no rights.