Leze
Leze

Reputation: 759

C# event, pass custom args to handler

Im looking for a way to do something like that

public void Download(string oid)
{
    Foo foo = Controller.Instance.GetFormation(oid);
    Request request = new Request (data);
    HttpResponseAction.Instance.OnFooDownloadedEvent += OnDoneDownloadAction(sender, event, request, foo);
}

private void OnDoneDownloadAction(object sender, OnLoadingEventArgs e, Request request, Foo foo)
{
      // Do something with my awesome args
}

So need to handle an event, attach an method to handle it, pass event args to this function plus some args in addition.

I need to do this in that way because event handler is added more than once and i need to remove it after trigger the good from the handled method.

How can i do this?

Upvotes: 1

Views: 1066

Answers (2)

ASh
ASh

Reputation: 35646

one solution that I can suggest now

public void Download(string oid)
{
    Foo foo = Controller.Instance.GetFormation(oid);
    Request request = new Request (data);

    // create a local variable of required delegate type   
    EventHandler<OnLoadingEventArgs> handler = 
    (object sender, OnLoadingEventArgs ev)  => 
    { 
         // foo and request can be used here
         // if foo or request is used here, then it became captured variable

         // Do something with your awesome captured variables: foo and request
    };

    // subscribe to event
    HttpResponseAction.Instance.OnFooDownloadedEvent += handler;

    // unsubscribe event handler when necessary
    HttpResponseAction.Instance.OnFooDownloadedEvent -= handler;
    // you might want to return handler from method or store it somewhere
}

editing your question I realized that you still can have a named method, if you like (but local variables got captured anyway)

public void Download(string oid)
{
    Foo foo = Controller.Instance.GetFormation(oid);
    Request request = new Request (data);

    // create a local variable of required delegate type   
    EventHandler<OnLoadingEventArgs> handler = 
    (object sender, OnLoadingEventArgs ev)  => 
    OnDoneDownloadAction(sender, ev, request, foo);

    // subscribe to event
    HttpResponseAction.Instance.OnFooDownloadedEvent += handler;

    // unsubscribe event handler when necessary
    HttpResponseAction.Instance.OnFooDownloadedEvent -= handler;
    // you might want to return handler from method or store it somewhere
}

private void OnDoneDownloadAction(object sender, OnLoadingEventArgs e, Request request, Foo foo)
{
      // Do something with my awesome args
}

Upvotes: 2

zackery.fix
zackery.fix

Reputation: 1816

First you need to define a delegate somewhere that represents the event handler:

// This needs to be inside a namespace...
public delegate void FooDownloadEventHandler(object sender, OnLoadingEventArgs e, Request request, Foo foo);

Then for OnFooDownloadEvent event member, you need to define using the delegate:

 public event FooDownloadEventHandler;

Subscribe to the event just as you did in your example code.

I hope this is what you were looking for....

https://msdn.microsoft.com/en-us/library/ms173171.aspx

https://msdn.microsoft.com/en-us/library/8627sbea.aspx

Upvotes: 0

Related Questions