Errore del compilatore CS0079

Aggiornamento: novembre 2007

Messaggio di errore

L'evento 'evento' può trovarsi soltanto sul lato sinistro di += o di -=.
The event 'event' can only appear on the left hand side of += or -=

Un evento è stato chiamato in modo non corretto. Per ulteriori informazioni, vedere Eventi (Guida per programmatori C#) e Delegati (Guida per programmatori C#).

Il seguente codice di esempio genera l'errore CS0079:

// CS0079.cs
using System;

public delegate void MyEventHandler();

public class Class1
{
   private MyEventHandler _e;

   public event MyEventHandler Pow
   {
      add
      {
         _e += value;
         Console.WriteLine("in add accessor");
      }
      remove
      {
         _e -= value;
         Console.WriteLine("in remove accessor");
      }
   }

   public void Handler()
   {
   }

   public void Fire()
   {
      if (_e != null)
      {
         Pow();   // CS0079
         // try the following line instead
         // _e();
      }
   }

   public static void Main()
   {
      Class1 p = new Class1();
      p.Pow += new MyEventHandler(p.Handler);
      p._e();
      p.Pow += new MyEventHandler(p.Handler);
      p._e();
      p._e -= new MyEventHandler(p.Handler);
      if (p._e != null)
      {
         p._e();
      }
      p.Pow -= new MyEventHandler(p.Handler);
      if (p._e != null)
      {
         p._e();
      }
   }
}