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.