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?
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.
Try this
public delegate void MyClassEHandler(MyClassEParam param);
static MyClassEHandler error;
public static event MyClassEHandler Error
{
add
{
MyClassE.error += (MyClassEHandler)Delegate.Combine(MyClassE.Error, value);
}
remove
{
MyClassE.Error -= (MyClassEHandler)Delegate.Combine(MyClassE.Error, value);
}
}
Refer to the Intercepting add remove of c# event and delegates
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.