Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# CS0079 Event Handling Compile Error

I got CS0079 compile error when I tried to run the code below:

public delegate void MyClassEHandler(MyClassEParam param);

public class MyClassE
{
    public static event MyClassEHandler Error
    {
         add
         {
              MyClassE.Error = (MyClassEHandler)Delegate.Combine(MyClassE.Error, value);
         } 
    }
}

Error:

CS0079 : The event MyClassE.Error can only appear on the left hand side of += or -=

Searched around but couldn't figure out how to resolve it.

ADDED: if (MyClass.Error != null) or MyClass.Error(null, null);

Get the same CS0079 error.

CS0079 : The event MyClassE.Error can only appear on the left hand side of += or -=

Can anyone help me on this?

like image 587
Chris Avatar asked Dec 20 '12 14:12

Chris


1 Answers

You cannot set an event, you can just add or remove handlers on it. So, as the error says, you should just do:

public delegate void MyClassEHandler(MyClassEParam param);

public static event MyClassEHandler Error
{
     add
     {
          MyClassE.Error += value;
     } 
     remove         
     {
          MyClassE.Error -= value;
     } 
}

and the Delegate.Combine will work magically for you.

like image 157
SWeko Avatar answered Oct 02 '22 04:10

SWeko