有条件的代表? [英] Conditional Delegate?

查看:113
本文介绍了有条件的代表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用户使用窗体f后,表单将保留一个值,然后再运行doStuff()。喜欢,如果f.value> 0,那么运行doStuff(),否则不要运行doStuff()。我最简洁地修改我的代码以允许这个检查?当代理人被分配时,我不太明白,如果我通过f.value,当我添加代理时,还是当它正在运行代理时,它会取值吗?

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();};

谢谢!

推荐答案

您可以在委托时捕获引用的值:

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

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

当事件发生时,它将检查捕获的引用的当前值 f ,如果条件匹配,则继续执行。

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

这篇关于有条件的代表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆