Event Handlers conceptually RRS feed

  • Question

  • User1094877758 posted

    It seems as if the point of event handlers is to associate the event with the delegate?  From the example on http://stackoverflow.com/questions/803242/understanding-events-and-event-handlers-in-c-sharp, it seems as if the purpose of the event handler is to associate the delegate MyEventHandler to HandleSomethingHappened.  Is that correct?

    public delegate void MyEventHandler(string foo);

    //This event can cause any method which conforms //to MyEventHandler to be called. public event MyEventHandler SomethingHappened;

    //Here is some code I want to be executed //when SomethingHappened fires.

    void HandleSomethingHappened(string foo) {     //Do some stuff }

    //I am creating a delegate (pointer) to HandleSomethingHappened //and adding it to SomethingHappened's list of "Event Handlers". myObj.SomethingHappened += new MyEventHandler(HandleSomethingHappened);

    Tuesday, June 25, 2013 9:02 PM

All replies

  • User1759999623 posted

    You raise the event to invoke the delegate to call the event handler from outside the object of the event.

    Tuesday, June 25, 2013 10:39 PM