2

I would like to create event actions to notify other classes when something happened. So my current flow looks like this

enter image description here

For testing purposes I created this code

Program.cs

Instantiate the first class and call a method from it (constructor is fine).

internal class Program
{
    private static void Main(string[] args)
    {
        First f = new First();
    }
}

First.cs

Instantiate the second class and call a method from it (constructor is fine). Listen for an event of the second class when some data has changed.

internal class First
{
    public First()
    {
        // ...

        Second s = new Second();
        s.Updated += OnSecondUpdated;
    }

    private void OnSecondUpdated()
    {
        Console.WriteLine("Done");
        Console.ReadLine();
    }
}

Second.cs

Instantiate the third class and call a method from it (constructor is fine). Listen for an event of the third class when some data has changed and raise the own one.

internal class Second
{
    public event Action Updated;

    public Second()
    {
        // ...

        Third t = new Third();
        t.Updated += OnThirdUpdated;
    }

    private void OnThirdUpdated()
    {
        // ...

        Updated();
    }
}

Third.cs Raise an event when some data has changed.

internal class Third
{
    public event Action Updated;

    public Third()
    {
        // ...

        Updated();
    }
}

Unfortunately the event variables are null. How can I instantiate these variables properly?

3
  • Take a look at the observer design pattern.
    – EzLo
    May 10, 2019 at 8:09
  • thank you very much but is it possible to fix the current code? I think there is a syntax problem but I didn't get it ..
    – user9945420
    May 10, 2019 at 8:14
  • You are first executing Update() and then linking the events because the logic is inside each constructor.
    – EzLo
    May 10, 2019 at 8:20

2 Answers 2

1

The problem here is that you're trying to do this in the constructor, where at that time nothing has (yet) been assigned to the Updated event. You can "solve" this by checking for null:

internal class Third
{
    public event Action Updated;

    public Third()
    {
        // ...
        if(Updated != null)
            Updated();
    }
}

But it wont mean your code now "works" as you only assign the event a handler after constructor has been called:

Third t = new Third();
t.Updated += OnThirdUpdated;

So one possible solution for this pattern is to NOT do this raising of the event in the constructor, and instead defer the logic to another method.

internal class Third
{
    public event Action Updated;

    public Third()
    {            
    }

    public void Init()
    {
        // ...
        if(Updated != null)
            Updated();
    }
}

Third t = new Third();
t.Updated += OnThirdUpdated;
t.Init();
1

You call the Update() before the classes can subscribe to the events, due to the constructor of the underlying object being called first. I changed it so that the constructor takes the related class and subscribes the event itself.

internal class First
{
    public First()
    {
        Second s = new Second(this);
    }

    internal void OnSecondUpdated()
    {
        Console.WriteLine("Done");
        Console.ReadLine();
    }
}

internal class Second
{
    public event Action Updated;

    public Second(First f)
    {
        Updated += f.OnSecondUpdated;
        Third t = new Third(this);
    }

    internal void OnThirdUpdated()
    {
        Updated();
    }
}

internal class Third
{
    public event Action Updated;

    public Third(Second s)
    {
        Updated += s.OnThirdUpdated;
        Updated();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.