Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action<object, EventArgs> could not be cast to EventHandler?

I was wiring up an event to use a lambda which needed to remove itself after triggering. I couldn't do it by inlining the lambda to the += event (no accessable variable to use to remove the event) so i set up an Action<object, EventArgs> variable and moved the lambda there. The main error was that it could not convert an Action<object, EventArgs> to an EventHandler. I thought lambda expressions were implicitly convertable to event handlers, why doesn't this work?

like image 834
RCIX Avatar asked Oct 11 '09 22:10

RCIX


People also ask

How do you declare an EventHandler?

Use the EventHandler delegate for all events that do not include event data. Use the EventHandler<TEventArgs> delegate for events that include data about the event. These delegates have no return type value and take two parameters (an object for the source of the event, and an object for event data).

What is EventArgs?

EventArgs e is a parameter called e that contains the event data, see the EventArgs MSDN page for more information. Object Sender is a parameter called Sender that contains a reference to the control/object that raised the event.

Is an EventHandler a delegate?

The EventHandler delegate is a predefined delegate that specifically represents an event handler method for an event that does not generate data. If your event does generate data, you must use the generic EventHandler<TEventArgs> delegate class.

What is event handler in C# with example?

An event handler is the subscriber that contains the code to handle specific events. For example, an event handler can be used to handle an event that occurs during the click of a command button in the UI. In C#, an event is connected to its handler by an event delegate.


1 Answers

Action<Object, EventArgs> a = (o, ea) => { }; EventHandler e = a.Invoke; 
like image 95
QrystaL Avatar answered Sep 23 '22 04:09

QrystaL