0
votes

I'm trying to raise events in my abstract class and my derived class and have them come out to the same event in main. Is this possible? With the code below, I set a breakpoint in the two raise events and I see the bar raise but never the foo and the actual event in main is never called due to OnDataReceived being null. What am I doing wrong? If I try to make the bar event abstract and the Raise Function virtual in bar and override both in my derived classes then I get an error for the OnDataReceived in bar not being left side of += or -=.

Here is basically what I have:

public abstract class bar
{
    public event DataReceivedHandler OnDataReceived;

    protected void RaiseDataReceivedEvent(EventArgs e)
    {
        if (OnDataReceived != null)
            OnDataReceived(this, e);
    }

    /// <summary>
    /// A global event that will happen for all "bar" derived classes
    /// </summary>
    private void globalEvent()
    {
        //Raise this event for the derived class here.
        RaiseDataReceivedEvent(new EventArgs());
    }
}

public class foo : bar
{
    public foo()
    {

    }

    /// <summary>
    /// A specific event that will happen only for this derived class
    /// </summary>
    private void fooSpecificEvent()
    {
        //Raise this event for the derived class here
        RaiseDataReceivedEvent(new EventArgs());
    }
}

public class Main
{
    bar specificProduct = new foo();

    public Main()
    {
        specificProduct.OnDataReceived += specificProduct_OnDataReceived;
    }

    void specificProduct_OnDataReceived(object sender, IttsDataReceivedEventArgs e)
    {
        //Here I want to process events from both fooSpecificEvent and globalEvent calls to RaiseDataReceivedEvent
    }
}
2
The abstract class contains global methods that all derived classes will use/call. However, I want the abstract class to manipulate the data for the global methods. The DataReceived event is used to notify consumer classes when new data is present. However, when I break on the Raise method, the OnDataReceived is always null. - Jeremy
When methods globalEvent & 'fooSpecificEvent' called? - Serj-Tm
Can you post a simple main program that demonstrates the problem? - BJ Myers

2 Answers

0
votes

You can change your method GlobalEvent() to protected and simply call it from the foo specific event. Something like that:

    private void fooSpecificEvent()
{
    //Raise this event for the derived class here
    RaiseDataReceivedEvent(new EventArgs());
    base.globalEvent();
}
0
votes

So, yeah... The above code works. Just make sure the calling foo object is the same as the foo that has been wired to the event. Stupid mistake.

I had foo_1.OnDataReceived wired but foo_2 was the event calling.