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

5

24

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

Trocar answered 14/8, 2013 at 14:15 Comment(2)
Where is the DataTable going to come from?Rotund
@PaulieWaulie its there where I am binding the event.Trocar
T
56

You can use anonymous delegate for that:

lnkSynEvent.Click += 
         new EventHandler((s,e)=>lnkSynEvent_Click(s, e, your_parameter));
Tweeze answered 14/8, 2013 at 14:19 Comment(5)
WOW worked like a charm, thanks @alex, tough I am not much famalier with anonymous delegates yet.Trocar
You should mark this answer as correct. This may help others.Leatriceleave
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);Zsa
Alex, and how can we remove that? simply with lnkSynEvent.Click -=new EventHandler( ... ?Cower
@alex: As T.Todua stated, how do you unsubscribe from it?Luanaluanda
A
8

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);
Anthropogeography answered 29/11, 2015 at 12:53 Comment(1)
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.Acrophobia
L
3

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

Listerism answered 20/10, 2021 at 12:17 Comment(0)
J
2

by use of delegate:

lnkbtnDel.Click += delegate(object s, EventArgs e1) { 
                 Dynamic_Click(s, e1, lnkbtnDel.ID); 
               };`  
Joinville answered 2/9, 2014 at 9:47 Comment(0)
V
2
EventHandler myEvent = (sender, e) => MyMethod(myParameter);//my delegate

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

private void MyMethod(MyParameterType myParameter)
{
 //Do something
}
Vlad answered 19/1, 2018 at 9:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.