I can't compile the following code.
Compile error CS0079 : The event 'CustomEvent' can only appear on the left hand side of += or -=
if (CustomEvent != null) //CS0079
CustomEvent(null, null); //CS0079
How can I make this work?
My implementation is like this:
public delegate void EventHandler(object sender, EventArgs e);
public static event EventHandler CustomEvent
{
add { CustomEvent += value; }
remove { CustomEvent -= value; }
}
private static void Func()
{
if (CustomEvent != null) //CS0079
CustomEvent(null, null); //CS0079
}
You can only test/invoke an event if it is a field-like event declared in the current type. So: there are two scenarios that would cause this:
it isn't a field-like event, but has custom add
/remove
accessors: in which case, only your custom code knows how the delegate is stored
it isn't declared in the current type, but is in a base-type or some unrelated object: in which case, you'll need to get the declaring type to invoke the event, usually via an OnCustomEvent
method. In the case of a base-type, the convention would be to make this method protected virtual
, which allows sub-classes to invoke the event and hook into the event via override
(comments)
It looks like the case1. however, I don't understand what to do to resolve this issue.
If you have custom add
/remove
, then how to invoke it is implementation-specific (I could tell you more if I could see the add
/remove
), but let's look at two common implementations:
1a: a backing delegate:
private EventHandler someEvent;
public event EventHandler SomeEvent
{
add { someEvent += value; }
remove { someEvent -= value; }
}
in this case, the "invoke" implementation would be simply:
if(someEvent != null) someEvent(this, EventArgs.Empty);
or if you are feeling extra-cautious:
var handler = someEvent;
if(handler != null) handler(this, EventArgs.Empty);
1b: an EventHandlerList
(used for sparse events):
private static readonly object SomeEventKey = new object();
public event EventHandler SomeEvent
{
add { Events.AddHandler(SomeEventKey, value); }
remove { Events.RemoveHandler(SomeEventKey, value); }
}
in which case the invoke implementation would be:
var handler = (EventHandler)Events[SomeEventKey];
if(handler != null) handler(this, EventArgs.Empty);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With