67

Let's say I want to pass some extra data when assigning an event handler. Consider the following code:

private void setup(string someData)
{
     Object.assignHandler(evHandler);
}

public void evHandler(Object sender)
{
    // need someData here!!!
}

How would I go about getting someData into my evHandler method?

Peter Duniho
  • 66,412
  • 6
  • 96
  • 128
Andy Hin
  • 27,603
  • 39
  • 97
  • 139
  • I have a more standard solution in this question, first answer http://stackoverflow.com/questions/14058412/passing-parameter-to-an-event-handler/14058441#14058441 – Sawan Nov 25 '13 at 14:09

8 Answers8

55
private void setup(string someData)
{
     Object.assignHandler((sender) => evHandler(sender,someData));
}
public void evHandler(Object sender, string someData)
{
    // need someData here!!!
}
spender
  • 112,247
  • 30
  • 221
  • 334
  • 2
    This is a great solution, but what if the EventHandler has TArgs already. Such as += new EventHandler(evHandler)? – Roast Dec 23 '11 at 13:58
  • Hi @Lily , it's not entirely clear what you are asking. It might be better to post a question (rather than a comment) with a bit more detail and some code to demonstrate what you're trying to do. – spender Dec 24 '11 at 02:34
  • 1
    Thanks, this solved a problem I was having, but I'm new to C# so would you mind explaining what this language construct is or giving me a keyword to google for an explanation? – Tom Smilack Apr 11 '12 at 16:44
  • @TomSmilack, take a look at lambda expressions and capturing variables with closures. Basically, lambda expressions are a shorthand way of declaring a method (...and so much more, but that's a different story) – spender Apr 11 '12 at 16:56
  • I have a more standard solution in this question, first answer: http://stackoverflow.com/questions/14058412/passing-parameter-to-an-event-handler/14058441#14058441 – Sawan Nov 25 '13 at 14:07
  • Hmmm, I see, then the problem here is to mark the other question as a duplicate to this question, but your answer is very good for this very specific situation, I've read the answer very quickly but haven't seen the event args class, however thank you for response and I will upvote the question whenever I can :) – Sawan Nov 26 '13 at 08:06
  • cn.InfoMessage += ((s, e) => { Console.WriteLine("[" + DBName + "] [Backup] " + e.Message); }); This is probably what most people are looking for. Where DBName is a local variable, which gets in the scope. – Shiroy Aug 23 '17 at 22:09
31

I had a hard time figuring out @spender's example above especially with: Object.assignHandler((sender) => evHandler(sender,someData)); because there's no such thing as Object.assignHandler in the literal sense. So I did a little more Googling and found this example. The answer by Peter Duniho was the one that clicked in my head (this is not my work):

snip

The usual approach is to use an anonymous method with an event handler that has your modified signature. For example:

void Onbutton_click(object sender, EventArgs e, int i) { ... }

button.Click += delegate(object sender, EventArgs e) 
{ Onbutton_click(sender, e, 172); };

Of course, you don't have to pass in 172, or even make the third parameter an int. :)

/snip

Using that example I was able to pass in two custom ComboBoxItem objects to a Timer.Elapsed event using lambda notation:

simulatorTimer.Elapsed +=
(sender, e) => onTimedEvent(sender, e,
(ComboBoxItem) cbPressureSetting.SelectedItem,
(ComboBoxItem) cbTemperatureSetting.SelectedItem);

and then into it's handler:

static void onTimedEvent(object sender, EventArgs e, ComboBoxItem pressure, ComboBoxItem temperature)
    {
        Console.WriteLine("Requested pressure: {0} PSIA\nRequested temperature: {1}° C", pressure, temperature);
    }

This isn't any new code from the examples above, but it does demonstrate how to interpret them. Hopefully someone like me finds it instructive & useful so they don't spend hours trying to understand the concept like I did.

This code works in my project (except for a non-thread-safe exception with the ComboBoxItem objects that I don't believe changes how the example works). I'm figuring that out now.

delliottg
  • 3,732
  • 2
  • 35
  • 48
  • 2
    The only problem with this is that if you want to unsubscribe the event (and most likely you should), you'd be unsubscribing via an anonymous delegate, which you should not be doing - see [this answer to a related question](https://stackoverflow.com/a/41199095/422845). – jbyrd Mar 29 '18 at 20:43
19

Captured variables:

private void setup(string someData)
{
    Object.assignHandler((sender,args) => {
        evHandler(sender, someData);
    });
}

public void evHandler(Object sender, string someData)
{
    // use someData here
}

Or (C# 2.0 alternative):

    Object.assignHandler((EventHandler)delegate(object sender,EventArgs args) {
        evHandler(sender, someData);
    });
Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
4

you can try doing this:

string yourObject;

theClassWithTheEvent.myEvent += (sender, model) =>
{
 yourObject = "somthing";
}
Idan
  • 499
  • 2
  • 10
  • 23
1

My question that was similar was marked a duplicate so thought I'd add an answer here since it won't let me on my question.

class Program
    {
        delegate void ComponentEventHandler(params dynamic[] args);

        event ComponentEventHandler onTest;

        static void Main(string[] args)
        {  
            Program prg = new Program();

            // can be bound to event and called that way
            prg.onTest += prg.Test;
            prg.onTest.Invoke("What", 5, 12.0);

            Console.ReadKey();
        }

        public void Test(params dynamic[] values)
        {
            // assign our params to variables
            string name = values[0];
            int age = values[1];
            double value = values[2];

            Console.WriteLine(name);
            Console.WriteLine(age);
            Console.WriteLine(value);
        }
    }
user441521
  • 6,644
  • 22
  • 83
  • 151
0

You could create a custom object having additional properties based on Object:

class CustomObject : Object
{
    public string SomeData;
}

private void setup(string someData)
{
    CustomObject customObject = new CustomObject { SomeData = someData };
    CustomObject.assignHandler(evHandler);
}

public void evHandler(Object sender)
{
    string someData = ((CustomObject)sender).SomeData;
}

If the data should not be changed anymore after initialization, you could also add a custom constructor, for example.

Michael Geier
  • 1,375
  • 14
  • 15
0

Here is my one-line solution that pass extra parameters to a timer handler.

private void OnFailed(uint errorCode, string message)
{
    ThreadPoolTimer.CreateTimer((timer) => {
    UI.ErrorMessage = string.Format("Error: 0x{0:X} {1}", errorCode, message);
    }, System.TimeSpan.FromMilliseconds(100));
}
Ramius
  • 111
  • 8
0

Well, the simplest method id to make someData a member variable like so:

public class MyClass
{
    private string _eventData;

    private void setup(string someData) 
    {
       _eventData = someData;
       Object.assignHandler(evHandler);
    }

    public void evHandler()
    {
        // do something with _eventData here
    }
}

I'm not sure that's the best way to do it, but it really depends on the event type, the object, etc.

CodingGorilla
  • 19,196
  • 3
  • 43
  • 62