如何在函数执行一次后将其删除

发布于 2024-11-29 11:11:16 字数 396 浏览 2 评论 0原文

我有一个要在单选按钮检查事件下执行的函数,但只需执行一次。在随后的单选按钮检查事件中,不应调用我的函数。

void rb_CheckedChanged(x, y)
{
    LoadSomeData(); //need to load only once.
    // rest of the code; to be executed every single time..
}

注意:

  1. 我希望仅在单选按钮事件下执行LoadSomeData()

  2. 我可以设置一个整数成员变量,并执行 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:

  1. I want LoadSomeData() to be executed only under radio button event.

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

寄居者 2024-12-06 11:11:16

您可以做的是创建一个要在事件中调用的函数名称列表,然后从列表中获取方法名称,使用反射调用您感兴趣的方法,然后从列表中删除该方法名称...下次,当再次调用该事件时,该方法不会在列表中之前被执行..类似于:

List<string> listMethod = new List<string>();
listMethod.Add("LoadSomeData");

then

void rb_CheckedChanged(x, y)
{
string methodName = listMethod[0];
Type.InvokeMember(
                    methodName,
                    BindingFlags.InvokeMethod | BindingFlags.Public | 
                        BindingFlags.Static,
                    null,
                    null,
                    null);
listMethod.Clear(); //or remove what ever you want
}

您可以从以下位置获取有关动态方法调用的信息: 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:

List<string> listMethod = new List<string>();
listMethod.Add("LoadSomeData");

then

void rb_CheckedChanged(x, y)
{
string methodName = listMethod[0];
Type.InvokeMember(
                    methodName,
                    BindingFlags.InvokeMethod | BindingFlags.Public | 
                        BindingFlags.Static,
                    null,
                    null,
                    null);
listMethod.Clear(); //or remove what ever you want
}

You can get information about dynamic method invoking from: http://www.codeproject.com/KB/cs/CallMethodNameInString.aspx

向日葵 2024-12-06 11:11:16

您可以设置隐藏字段并使用它作为标志来确定是否应该再次运行 LoadSomeData。类似于:

void rb_CheckedChanged(x, y)
{
    if (hiddenField.Value == "False")
    {
        LoadSomeData(); //need to load only once.
        hiddenField.Value = true.ToString();
    }
    else
    {
         // Do nothing.
    }
    // rest of the code; to be executed every single time..
}

在您的页面中:

<asp:HiddenField ID="hiddenField" Value="False" />

You could set a hidden field and use that as a flag to determine whether you should run LoadSomeData again. Something like:

void rb_CheckedChanged(x, y)
{
    if (hiddenField.Value == "False")
    {
        LoadSomeData(); //need to load only once.
        hiddenField.Value = true.ToString();
    }
    else
    {
         // Do nothing.
    }
    // rest of the code; to be executed every single time..
}

And in your page:

<asp:HiddenField ID="hiddenField" Value="False" />
逆光飞翔i 2024-12-06 11:11:16

我认为选项 2 是你最好的选择,周围有一个粘性布尔值。

private bool stickyFlag = true;

void rb_CheckedChanged(x,y)
{
    if (true == stickyFlag)
    {
        LoadSomeData();
        stickyFlag = false;
    }
    ....
    // rest of the code; to be executed every single time..
}

i think option 2 is your best choice, have a sticky bool around it.

private bool stickyFlag = true;

void rb_CheckedChanged(x,y)
{
    if (true == stickyFlag)
    {
        LoadSomeData();
        stickyFlag = false;
    }
    ....
    // rest of the code; to be executed every single time..
}
乱世争霸 2024-12-06 11:11:16

您可以使用 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

遗失的美好 2024-12-06 11:11:16

我会选择第二个选项,但你可以使用委托:

public event Action doLoadSomeData; 

void onLoad(sender, args)
{
    doLoadSomeData += LoadSomeData();
}

void rb_CheckedChanged(x, y)
{
    if (doLoadSomeData != null)
    {
         doLoadSomeData(); //need to load only once.
    }
    doLoadSomeData = null;
    // rest of the code; to be executed every single time..
}

i'd go for the second option, but you could use delegates:

public event Action doLoadSomeData; 

void onLoad(sender, args)
{
    doLoadSomeData += LoadSomeData();
}

void rb_CheckedChanged(x, y)
{
    if (doLoadSomeData != null)
    {
         doLoadSomeData(); //need to load only once.
    }
    doLoadSomeData = null;
    // rest of the code; to be executed every single time..
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文