-1

So I have looked at many places and what I am trying to do is not working, although I seem to be doing what everywhere else says I should. I have the following:

    private EventHandler statsUpdated;
    public event EventHandler StatsUpdated
    {
        add
        {
            if (statsUpdated == null || !statsUpdated.GetInvocationList().Contains(value))
            {
                statsUpdated += value;
            }
        }
        remove
        {
            statsUpdated -= value;
        }
    }

    protected virtual void OnStatsUpdated(EventArgs e)
    {
        EventHandler Handler = StatsUpdated;
    }

I havent started doing any more to the OnStatsUpdated method yet, as the line in there is erroring saying that StatsUpdated can only be on the left of += or -=. However, I am accessing it from the same class? The above is a direct copy paste, they sit directly together in code right now.

What am I doing wrong?

pingu2k4
  • 956
  • 2
  • 15
  • 31

2 Answers2

0

I think it is typo mistake. Just change last line to EventHandler Handler = statsUpdated;

Jerry Switalski
  • 2,690
  • 1
  • 18
  • 36
0

In fact you are not firing the event. To Fire it, you should use

protected virtual void OnStatsUpdated(EventArgs e)
{
   var handler = statsUpdated;
   if (handler != null)
      handler (this, e);
}

If you're using C# 6 you can simplify it to

protected virtual void OnStatsUpdated(EventArgs e)
{
   statsUpdated?.Invoke(this, e);
}