How to fix this error: "MyEvent + = expected"?

I have a delegate, say:

public delegate void MyDelegate();

      

I have an event, say:

public MyDelegate MyEvent;

      

When I call the event, I get the error:

"MyEvent + = pending ....."

How do I resolve this?

+1


source to share


5 answers


You can only call the event from the class in which you declared it. Elsewhere, you can add or remove handlers from the event delegate through statements +=

and -=

hence the error message.



+4


source


The + = is related to events, not just the delegate declaration. You are missing the keyword 'event'.

public **event** MyDelegate MyEvent;

      



Once you have that keyword, the + = function will work for you.

+4


source


If you are trying to use an event from another class, you need to understand the difference between events and delegates. The event simply encapsulates the "subscribe" and "unsubscribe" aspects, not "raise the event". (In fact, in IL, you can have a member for "raise an event", but C # doesn't support it.)

See my article on Events and Delegates for details .

+1


source


Also you can have a look at this post about avoiding null delegate checking

0


source


Check out Chris Sells .NET Delegates: C # Bedtime Story for a great guide to delegates and events. Informative and quite entertaining.

0


source







All Articles