Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional Delegate?

After the user is done with form "f", the form will retain a value that I want to check before running doStuff(). Like, if f.value > 0 , then run doStuff(), else, don't run doStuff(). How can I most concisely modify my code to allow for this check? I don't quite understand when the delegate is assigned, if I pass f.value, will it take the value when I'm adding the delegate, or when it is running the delegate?

form f = new form();
f.Show();
f.FormClosing += delegate{doStuff();};

Thanks!

like image 280
sooprise Avatar asked Sep 13 '26 22:09

sooprise


1 Answers

You can capture the value of the reference when making the delegate:

f.FormClosing += delegate { if(f.value > 0) doStuff(); };

When the event occurs, it will check the current value of the captured reference f, and if the condition matches, continue executing.

like image 121
Tejs Avatar answered Sep 16 '26 11:09

Tejs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!