开发者

How to remove a function after it has been executed once

开发者 https://www.devze.com 2023-03-28 06:58 出处:网络
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.

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 intege开发者_Python百科r member variable, and do count++, n check if == 1 etc. Or a boolean flag. That's not what I'm looking for.

Thanks..


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


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 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..
}


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:

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..
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号