0

I am very new to using C# event handling. Our application has the following events defined:

public sealed class MonitorCallback : IMonitorCallback
{
    public event EventHandler<ApplyEventArgs> ApplyAccepted;
    public event EventHandler<ApplyEventArgs> ApplyRejected;
}

I need to write some code to handle and respond these events when they are fired. Could someone get me started as to how I might do this?

1
  • 2
    while I'm not adverse to helping you, couldn't you ask the person in your team who wrote them? Commented Apr 4, 2011 at 14:14

4 Answers 4

2

Visual Studio will automatically create stubs for the event handler function when you start typing the += below and hit TAB.

protected MyMonitorCallback MonitorCallback;
public class MyClass
{
     void Main()
     {
          MyMonitorCallback = new MonitorCallback();
          MyMonitorCallback.ApplyAccepted += new EventHander<ApplyEventArgs>(MyMonitorCallback_ApplyAccepted);
     }
     void MyMonitorCallback_ApplyAccepted(object sender, ApplyEventArgs e) {
        ..
     }
}
Sign up to request clarification or add additional context in comments.

1 Comment

An EventHander<T> delegate also has a sender attribute... So your code should be MyMonitorCallback_ApplyAccepted(object sender, ApplyEventArgs e)
2

The best place to start would be the msdn tutorial This will go through declaring, invoking and hooking up an event.

It might also be a good idea to have a read about delegates (another msdn tutorial) as you would use them in your event handling and it would be a good idea to get an understanding from the ground up.

Comments

0

When you create the MonitorCallback instance, you subscribe the events to the event handler that you are going to write. Syntax would look something like,

 public class MonitorCallbackFactory{

     public MonitorCallback CreateCallback(){
         //  create the callback instance
         var callback = new MonitorCallback();

         // subscribe the events to the EventHandler.  
         callback.ApplyAccepted += OnApplyAccepted;
         callback.ApplyRejected += OnApplyRejected;

         return callback;
     } 

     protected virtual void OnApplyAccepted(object sender, ApplyEventArgs e){
           // the sender is always the type of object that raises the event, so
           // if you need it strongly typed you can do:
           var callback = (MonitorCallback)sender;
           // then write your code for what happens when
           // the ApplyAccepted event is raised here  
     }

     protected virtual void OnApplyRejected(object sender, ApplyEventArgs e){
           // write your code for what happens when
           // the ApplyRejected event is raised here  
     }

 }

As you can see the += is the syntax to subscribe a handler to an event. -= is the syntax to unsubscribe and event handler.

Comments

0

I presume you already have code like this;

public class EventExample
{
    private EventHandler<ApplyEventArgs> m_evApplyAccepted;
    public event EventHandler<ApplyEventArgs> ApplyAccepted
    {
        add { m_evApplyAccepted += value; }
        remove { m_evApplyAccepted -= value; }
    }
    protected virtual void OnEventName(ApplyEventArgs e)
    {
        if (m_evApplyAccepted != null)
            m_evApplyAccepted.Invoke(this, e);
    }
    public class ApplyEventArgs : EventArgs { }
}

You consume this event like this;

public class EventConsumer
{
    private EventExample eventExample;
    public EventConsumer()
    {
        eventExample = new EventExample();

        //register a handler with the event here
        eventExample.ApplyAccepted += new EventHandler<EventExample.ApplyEventArgs>(eventExample_EventName);
    }

    void eventExample_EventName(object sender, EventExample.ApplyEventArgs e)
    {
        //respond to event here
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.