C# pass additional parameter to an event handler while binding the event at the run time

30,282

Solution 1

You can use anonymous delegate for that:

lnkSynEvent.Click += 
         new EventHandler((s,e)=>lnkSynEvent_Click(s, e, your_parameter));

Solution 2

I don't know exactly when it's changed, but now it's even easier!

lnkSynEvent.Click += (s,e) => lnkSynEvent_Click(s, e, your_parameter);

Solution 3

EventHandler myEvent = (sender, e) => MyMethod(myParameter);//my delegate

myButton.Click += myEvent;//suscribe
myButton.Click -= myEvent;//unsuscribe

private void MyMethod(MyParameterType myParameter)
{
 //Do something
}

Solution 4

by use of delegate:

lnkbtnDel.Click += delegate(object s, EventArgs e1) { 
                 Dynamic_Click(s, e1, lnkbtnDel.ID); 
               };`  

Solution 5

All answers above seem to be fine, but I have discovered one pitfall which is not so obvious and it took some time to figure out what is going on, so I wanted to share it.

Assume that myList.Count returns 16.

In the following case, OnValueChangeWithIndex(p1, p2, i) will always be called with i = 16.

for (int i = 0; i < myList.Count; i++)
{
    myList[i].OnValueChange += (p1, p2) => OnValueChangeWithIndex(p1, p2, i);
}

To avoid that, you would need to initialize a new variable inside of the loop, then pass the new variable to the function.

for (int i = 0; i < myList.Count; i++)
{
    int index = i;
    myList[i].OnValueChange += (p1, p2) => OnValueChangeWithIndex(p1, p2, index);
}

Closures close over variables, not over values.

Closing over the loop variable considered harmful, part one

Share:
30,282

Related videos on Youtube

foo-baar
Author by

foo-baar

Updated on March 04, 2022

Comments

  • foo-baar
    foo-baar about 2 years

    I have a link button which have a regular click event :

    protected void lnkSynEvent_Click(object sender, EventArgs e)
    {
    }
    

    And I bind this event at the runtime :

    lnkSynEvent.Click += new EventHandler(lnkSynEvent_Click);
    

    Now I need the function to accept additional argument:

    protected void lnkSynEvent_Click(object sender, EventArgs e, DataTable dataT)
    {
    }
    

    And pass the same as parameter while binding this event :

    lnkSynEvent.Click += new EventHandler(lnkSynEvent_Click, //somehow here);
    

    Not sure how to achieve this. Please help.

    Thanks in advance.

    Vishal

    • Paulie Waulie
      Paulie Waulie almost 11 years
      Where is the DataTable going to come from?
    • foo-baar
      foo-baar almost 11 years
      @PaulieWaulie its there where I am binding the event.
  • foo-baar
    foo-baar almost 11 years
    WOW worked like a charm, thanks @alex, tough I am not much famalier with anonymous delegates yet.
  • Microsoft DN
    Microsoft DN almost 11 years
    You should mark this answer as correct. This may help others.
  • Alexandr Sargsyan
    Alexandr Sargsyan over 8 years
    Simply you can write like this Object.SomeEvent+= (sender,e)=>SomeEventHandler(sender, e, "Data"); /// And define your event handler like this void SomeEventHandler(object sender, SomeEventArgs e, string data);
  • Stunt
    Stunt about 8 years
    This actually worked better in my scenario as i needed to map an expected EventHandler<a,b> to EventHandler<a,b,c>. The accepted answer did not resolve this scenario.
  • T.Todua
    T.Todua over 6 years
    Alex, and how can we remove that? simply with lnkSynEvent.Click -=new EventHandler( ... ?
  • testing
    testing almost 6 years
    @alex: As T.Todua stated, how do you unsubscribe from it?