Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass extra parameters to an event handler?

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?

like image 403
Andy Hin Avatar asked Nov 18 '10 14:11

Andy Hin


People also ask

Is it possible to pass an additional parameter to an event handler?

Of course you can. (If it was the event you define yourself, you would need to create you own event arguments class where you would put all the information you need.

How many ways the parameters can be passed?

Parameters can be passed to a method in following three ways : Value Parameters. Reference Parameters. Output Parameters.

Can events have access modifiers?

Events can be marked as public, private, protected, internal, protected internal, or private protected. These access modifiers define how users of the class can access the event.


2 Answers

private void setup(string someData) {      Object.assignHandler((sender) => evHandler(sender,someData)); } public void evHandler(Object sender, string someData) {     // need someData here!!! } 
like image 61
spender Avatar answered Oct 24 '22 12:10

spender


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.

like image 37
delliottg Avatar answered Oct 24 '22 13:10

delliottg