tags:

views:

385

answers:

4

Hi,

I subscribe to an Event inside a class. Such as

MainStation mainStation = StationFactory.GetMainStation();  
mainStation.FrequencyChanged += new EventArgs(MainStation_FrequencyChanged);

My MainStation class raises the event on some condition by just calling the event FrequencyChanged()

The Problem

Now I have a scenario where I must instantiate SubStation from MainStation which is also a subclass of MainStation with some additional features and FrequencyChanged event must be subscribed as the MainStation subscrbed. Consider the code noted below:

public class MainStation
{
    public event EventHandler FrequencyChanged;
    public static SubStation CreateSubStation()
    {
        SubStation subStation = new SubStation();
        //here I want to pass/bubble FrequencyChanged event to SubStation
        subStation.FrequencyChanged = FrequencyChanged; //THIS IS WRONG
    }
}

Bottom Line
I want to fire an event that a class subscribes from another class, also bubble up events

Update
StationFactory creates MainStation and the FrequencyChanged event in MainStation instance is set as defined in the first code block.

A: 

How is the event getting fired? If MainStation is calling the event, use a form of event dispatch to cause it in SubStation as well. For example:

public class MainStation
{
    public event EventHandler FrequencyChanged;

    protected virtual void OnFrequencyChanged(EventArgs e)
    {
        if (null != this.FrequencyChanged) // Always have this to avoid exceptions
        {
            this.FrequencyChanged(this, e);
        }
    }
}

public class SubStation : MainStation
{
    protected override void OnFrequencyChanged(EventArgs e)
    {
        // Handle the event from MainStation here, then dispatch to MainStation
        // ....
        base.OnFrequencyChanged(e);
    }
}

Have MainStation always call OnFrequencyChanged whenever it needs to fire the FrequencyChanged event. This will call OnFrequencyChanged in SubStation if you are in fact dealing with a SubStation object.

Matthew Ferreira
`OnFrequencyChanged` does not belong to `MainStation` nor `SubMainStation`. There goes the problem :(
Munim Abdul
Try fixing your initial question then, because your given code would say otherwise.
Matthew Ferreira
+5  A: 

If FrequencyChanged does not belong to MainStation, but rather to some Base, you're going to have to chain and expose the event you're interested in.

public class MainStation : Base
{
    public event EventHandler StationFrequencyChanged;

    public MainStation()
    {
        // ...

        this.FrequencyChanged += new EventHandler(MainStation_FrequencyChanged);
    }

    void MainStation_FrequencyChanged(object sender, EventArgs e)
    {
        if (StationFrequencyChanged != null)
            StationFrequencyChanged(sender, e);
    }

    public void GetEventsFrom(MainStation src)
    {
        //this is where you assign src events to your object
        this.StationFrequencyChanged = src.StationFrequencyChanged;
    }

    public static SubStation CreateSubStation(MainStation main)
    {
        SubStation subStation = new SubStation();

        //register events    
        subStation.GetEventsFrom(main);

        return subStation;
    } 
}

public class SubStation : MainStation
{

} 
Dynami Le Savard
Thanks Dynami for your reply! Seems like this is what I wanted. I cannot test the code right now cuz I am on the move. I will test it as soon as possbile and let you know. Thanks again!
Munim Abdul
A: 

If you need something more flexible, it is possible to use WPF RoutedEvents.

Or implement similar approach manually: organize objects into tree and write an event manager that can route events upwards from tree node. If tree hierarchy fits your needs, it will be more convenient and predictable than forwarding every individual event in handler.

ima
A: 

If your CreateSubStation method were not static, then the code you have would work as expected. (Provided that the event handlers on MainStation were setup before you created Substation...which isn't really a great design, IMO.)

Eric Dahlvang