C#: Raising an inherited event

C#EventsInheritance

C# Problem Overview


I have a base class that contains the following events:

public event EventHandler Loading;
public event EventHandler Finished;

In a class that inherits from this base class I try to raise the event:

this.Loading(this, new EventHandler()); // All we care about is which object is loading.

I receive the following error:

The event 'BaseClass.Loading' can only appear on the left hand side of += or -= (BaseClass')

I am assuming I cannot access these events the same as other inherited members?

C# Solutions


Solution 1 - C#

What you have to do , is this:

In your base class (where you have declared the events), create protected methods which can be used to raise the events:

public class MyClass
{
   public event EventHandler Loading;
   public event EventHandler Finished;

   protected virtual void OnLoading(EventArgs e)
   {
       EventHandler handler = Loading;
       if( handler != null )
       {
           handler(this, e);
       }
   }

   protected virtual void OnFinished(EventArgs e)
   {
       EventHandler handler = Finished;
       if( handler != null )
       {
           handler(this, e);
       }
   }
}

(Note that you should probably change those methods, in order to check whether you have to Invoke the eventhandler or not).

Then, in classes that inherit from this base class, you can just call the OnFinished or OnLoading methods to raise the events:

public AnotherClass : MyClass
{
    public void DoSomeStuff()
    {
        ...
        OnLoading(EventArgs.Empty);
        ...
        OnFinished(EventArgs.Empty);
    }
}

Solution 2 - C#

You can only access an event in the declaring class, as .NET creates private instance variables behind the scenes that actually hold the delegate. Doing this..

public event EventHandler MyPropertyChanged;

is actually doing this;

private EventHandler myPropertyChangedDelegate;

public event EventHandler MyPropertyChanged
{
    add { myPropertyChangedDelegate += value; }
    remove { myPropertyChangedDelegate -= value; }
}

and doing this...

MyPropertyChanged(this, EventArgs.Empty);

is actually this...

myPropertyChangedDelegate(this, EventArgs.Empty);

So you can (obviously) only access the private delegate instance variable from within the declaring class.

The convention is to provide something like this in the declaring class..

protected virtual void OnMyPropertyChanged(EventArgs e)
{
    EventHandler invoker = MyPropertyChanged;

    if(invoker != null) invoker(this, e);
}

You can then call OnMyPropertyChanged(EventArgs.Empty) from anywhere in that class or below the inheritance heirarchy to invoke the event.

Solution 3 - C#

> I am assuming I cannot access these events the same as other inherited members?

Precisely. It's customary to provide a protected function OnXyz or RaiseXyz for each event in the base class to enable raising from inherited classes. For example:

public event EventHandler Loading;

protected virtual void OnLoading() {
    EventHandler handler = Loading;
    if (handler != null)
        handler(this, EventArgs.Empty);
}

Called in the inherited class:

OnLoading();

Solution 4 - C#

You can try this way, It works for me:

public delegate void MyEventHaldler(object sender, EventArgs e);

public class B
{
    public virtual event MyEventHaldler MyEvent;
    protected override void OnChanged(EventArgs e)
    {
        if (MyEvent != null)
            MyEvent(this, e);
    }
}

public class D : B
{
    public override event MyEventHaldler MyEvent;
    protected override void OnChanged(EventArgs e)
    {
        if (MyEvent != null)
            MyEvent(this, e);
    }
}

Solution 5 - C#

not to resurrect an old thread but in case anybody is looking, what I did was

protected EventHandler myPropertyChangedDelegate;

public event EventHandler MyPropertyChanged
{
    add { myPropertyChangedDelegate += value; }
    remove { myPropertyChangedDelegate -= value; }
}

This lets you inherit the event in a derived class so you can invoke it without requiring to wrap the method while keeping the += syntax. I guess you could still do that with the wrapping methods if you did

public event EventHandler MyPropertyChanged
{
   add { AddDelegate(value); }
   remove { RemoveDelegate(value); }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjwarzechView Question on Stackoverflow
Solution 1 - C#Frederik GheyselsView Answer on Stackoverflow
Solution 2 - C#Adam RobinsonView Answer on Stackoverflow
Solution 3 - C#Konrad RudolphView Answer on Stackoverflow
Solution 4 - C#Olioul Islam RahiView Answer on Stackoverflow
Solution 5 - C#ZeraphilView Answer on Stackoverflow