如何在函数执行一次后将其删除
我有一个要在单选按钮检查事件下执行的函数,但只需执行一次。在随后的单选按钮检查事件中,不应调用我的函数。
void rb_CheckedChanged(x, y)
{
LoadSomeData(); //need to load only once.
// rest of the code; to be executed every single time..
}
注意:
我希望仅在单选按钮事件下执行
LoadSomeData()
。我可以设置一个整数成员变量,并执行 count++、n 检查 if == 1 等。或者布尔标志。这不是我想要的。
谢谢..
I have a function to be executed under a radio button checked event, but it has to be done only once. In subsequent radio button checked events, my function shouldn't be called.
void rb_CheckedChanged(x, y)
{
LoadSomeData(); //need to load only once.
// rest of the code; to be executed every single time..
}
Note:
I want
LoadSomeData()
to be executed only under radio button event.I can set an integer member variable, and do count++, n check if == 1 etc. Or a boolean flag. That's not what I'm looking for.
Thanks..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以做的是创建一个要在事件中调用的函数名称列表,然后从列表中获取方法名称,使用反射调用您感兴趣的方法,然后从列表中删除该方法名称...下次,当再次调用该事件时,该方法不会在列表中之前被执行..类似于:
then
您可以从以下位置获取有关动态方法调用的信息: http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx
What you can do is create a list of function names that you would like to call in your event, then get the method name from list, using reflection call the method you are interested in and then remove that method name from the list... Next time, when the event is called again, the method won't be executed before it won't be in the list.. something like:
then
You can get information about dynamic method invoking from: http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx
您可以设置隐藏字段并使用它作为标志来确定是否应该再次运行
LoadSomeData
。类似于:在您的页面中:
You could set a hidden field and use that as a flag to determine whether you should run
LoadSomeData
again. Something like:And in your page:
我认为选项 2 是你最好的选择,周围有一个粘性布尔值。
i think option 2 is your best choice, have a sticky bool around it.
您可以使用 PostSharp [RunOnce] 创建一个属性,
该属性可能有一个属性,指示该方法是否已被触发
PostSharp Link< /a>
You could create a Attribute with PostSharp [RunOnce]
this attribut may have a property which indicates wether the method has already been triggered
PostSharp Link
我会选择第二个选项,但你可以使用委托:
i'd go for the second option, but you could use delegates: