|
-
Oct 27th, 2007, 06:30 AM
#1
Thread Starter
Member
[2.0] Inherited Events
Good day,
I`ve encountered a slight annoyance which, although it can easily be bypassed, I would much rather dispose of altogether.
Take the following two classes:
Code:
class BaseClass
{
public event EventHandler BaseEvent;
protected virtual event EventHandler VirtualEvent;
void SomeBaseMethod()
{
this.BaseEvent(this, EventArgs.Empty);
}
}
class DerivedClass : BaseClass
{
protected override event EventHandler VirtualEvent;
void SomeDerivedMethod()
{
this.VirtualEvent(this, EventArgs.Empty);
this.BaseEvent(this, EventArgs.Empty);
}
}
The above code shows two ways I tried using to the effect of generating a base-class event from a derived class.
Both ways yielded problems:
- VirtualEvent :
It seems that generating an overridden event does not cause the base event to be raised, but a different one altogether. At least, that is how it seems, since I had registered methods to the base event, but upon raising of the derived event, a NullReferenceException was thrown, which indicates that no handlers were ever assigned to the raised event. - BaseEvent :
This method does not even pass compilation.
C# enforces the 'event' access modifier even with inheriting classes, so that the event itself can only be initiated from context of the base class.
The only thing that can be done from within the context of a derived class is registration-type calls (using the '+=' and '-=' operators).
The bypassing I have mentioned earlier involves creating a Protected method in the base class with argument types that coninside with the event arguments, and whose sole purpose is to raise teh event.
In other words, something like:
base.RaiseBaseEvent(senderArgument, eventArgsArgument);
I would much appreciate it if anyone could point out a better way to accomplish the goal I have described here.
Thanks in advance.
-
Oct 27th, 2007, 06:52 PM
#2
Re: [2.0] Inherited Events
This is how you are supposed to do it:
CSharp Code:
public class MyEventArgs : EventArgs
{
// ...
}
public class BaseClass
{
public event EventHandler<MyEventArgs> MyEvent;
protected virtual void OnMyEvent(MyEventArgs e)
{
if (this.MyEvent != null)
{
this.MyEvent(this, e);
}
}
}
public class DerivedClass : BaseClass
{
protected override void OnMyEvent(MyEventArgs e)
{
// Code to execute before raising event here.
base.OnMyEvent(e);
// Code to execute after raising event here.
}
}
The method that raises the event is declared 'protected' so it can be accessed within derived classes but from outside. It is also declared "virtual' so it can be overridden in derived classes to customise behaviour on that event. It is usually the case, although not always, that you call the base class's implementation because that's where the event actually gets raised.
Note also that the method that raises the event has the same name prefixed with "On". This is the way it's done throughout the .NET Framework and you should use that convention for consistency.
Finally, note that I have used the generic EventHandler delegate. You can still declare your own type if you want but, from .NET 2.0, it's easier to use the existing generic delegate.
Last edited by jmcilhinney; Oct 27th, 2007 at 06:58 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|