Hide an event in an inherited class
-
Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event
SelectedIndexChanged
to be "disabled" out of MyCombo class. So that it shloud be impossible to write:MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged);
I tried this:**private new** event EventHandler SelectedIndexChanged;
but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, Bobo -
Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event
SelectedIndexChanged
to be "disabled" out of MyCombo class. So that it shloud be impossible to write:MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged);
I tried this:**private new** event EventHandler SelectedIndexChanged;
but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, BoboI havn't heard of a way to do this, maybe you could mark the event as Obsolete?
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed
-
Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event
SelectedIndexChanged
to be "disabled" out of MyCombo class. So that it shloud be impossible to write:MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged);
I tried this:**private new** event EventHandler SelectedIndexChanged;
but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, Bobobobo.jede wrote:
Is there any way to do this?
Yes, write your own control, rather than inheriting ComboBox.
bobo.jede wrote:
And is it correct to do it in OOP?
Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.
-
I havn't heard of a way to do this, maybe you could mark the event as Obsolete?
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed
-
bobo.jede wrote:
Is there any way to do this?
Yes, write your own control, rather than inheriting ComboBox.
bobo.jede wrote:
And is it correct to do it in OOP?
Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.
J4amieC wrote:
Yes, write your own control, rather than inheriting ComboBox.
Yes, that can be a way :) but I want to use this control and it's rich functionality.
J4amieC wrote:
Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.
But I can do the same with methods, don't I ?! Bobo
-
J4amieC wrote:
Yes, write your own control, rather than inheriting ComboBox.
Yes, that can be a way :) but I want to use this control and it's rich functionality.
J4amieC wrote:
Quite simply, no it is not. Inheritance implies that the derived object inherits every public property/method/event from its parent.
But I can do the same with methods, don't I ?! Bobo
bobo.jede wrote:
But I can do the same with methods, don't I
No, you can never completly hide a method in a derived class either. I suppose you could 'new' it and just do nothing within the method, but its still there.
-
Hi, I have an interesting problem (interesting for me :)). I need to hide an event in an inherited class. Example: Base class: ComboBox Inherited class: MyCombo And I want the event
SelectedIndexChanged
to be "disabled" out of MyCombo class. So that it shloud be impossible to write:MyCombo mc = new MyCombo(); mc.SelectedIndexChanged += new EventHandler(mc_SelectedIndexChanged);
I tried this:**private new** event EventHandler SelectedIndexChanged;
but it doesn't work - it is still possible to use this event. Is there any way to do this? And is it correct to do it in OOP? Thanx, BoboTry overriding OnSelectedIndexChanged and DONT call the base class.
**
xacc.ide-0.2.0 preview - Now in 100% C# goodness
**
-
Try overriding OnSelectedIndexChanged and DONT call the base class.
**
xacc.ide-0.2.0 preview - Now in 100% C# goodness
**
-
You mean Obsolete attribute? I've tried it, but it didn't help :( Thanx for your answer Bobo
bobo.jede wrote:
You mean Obsolete attribute?
Yes
bobo.jede wrote:
I've tried it, but it didn't help
I know it won't but it informs other developers that you shouldn't use it. Maybe if you mark it with this and then in the event throw a
NotImplementedException
. The obsolete gives them the reason why they should not use it. The only (and best way) to do this would be to write your own control (NB you don't have to write it from scratch you could create a wrapper around an existing ComboBox, only exposing the methods / events / properties you want).
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed
-
You mean Obsolete attribute? I've tried it, but it didn't help :( Thanx for your answer Bobo
Make your new event public and use ObsoleteAttribute with two parameters. In this case compiler will return an error when somebody try to use this event.
[Obsolete("SelectedIndexChanged is obsolete. Use MyNewEvent instead.", true)]
public new event EventHandler SelectedIndexChanged;