Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何了解在for循环期间运行的方法?_C#_Loops - Fatal编程技术网

C# 如何了解在for循环期间运行的方法?

C# 如何了解在for循环期间运行的方法?,c#,loops,C#,Loops,我有一个for循环,每次通过调用相同的方法。我需要找到一种方法来了解前面的等式是什么。我必须在不使用递增值的情况下找到这个。例如: for (int i = 0; i < 101; i++) { checkBox[i].Click += new System.EventHandler(checkBoxMethod); } 在for循环内部,还设置每个复选框的标记。我假设您在这里使用的是Windows窗体。下面是修改后的for循环的样子: for (int i = 0; i &l

我有一个
for
循环,每次通过调用相同的方法。我需要找到一种方法来了解前面的等式是什么。我必须在不使用递增值的情况下找到这个。例如:

for (int i = 0; i < 101; i++) { 
    checkBox[i].Click += new System.EventHandler(checkBoxMethod); }

在for循环内部,还设置每个复选框的标记。我假设您在这里使用的是Windows窗体。下面是修改后的for循环的样子:

for (int i = 0; i < 101; i++) {
    checkBox[i].Click += new System.EventHandler(checkBoxMethod);
    checkBox[i].Tag = i;
}

无论在创建该复选框的事件处理程序时“i”是什么,都将在变量“number”中检索,您可以随意使用它。

在for循环中,还可以设置每个复选框的标记。我假设您在这里使用的是Windows窗体。下面是修改后的for循环的样子:

for (int i = 0; i < 101; i++) {
    checkBox[i].Click += new System.EventHandler(checkBoxMethod);
    checkBox[i].Tag = i;
}

无论在创建该复选框的事件处理程序时“i”是什么,都将在变量“number”中检索,您可以随意使用它。

使用递归并将当前计数传递给函数,而不是使用
for
循环:

void checkBoxMethod (object sender, EventArgs args)
{
    CheckBox box = (CheckBox)sender;
    int number = (int)box.Tag;
    myRecursiveMethod(number);
}

private void myRecursiveMethod(int count)
{
    //do whatever stuff I need to do

    if (!timeToExitTheMethod)
        myRecursiveMethod(count++);
}
你没有给我们解释你在
for
循环中做了什么,你的问题没有太多意义(例如,你提到的复选框是什么?),因此我不能非常具体地介绍我的代码示例。请注意,您必须为递归方法编写一个退出点(否则将调用它,直到出现堆栈溢出)

如果您只是想计算函数被调用的次数,请执行以下操作:

public class MyClass
{
    private int _myCounter = 0;

    void checkBoxMethod (object sender, EventArgs args)
    {
        CheckBox box = (CheckBox)sender;

        //do whatever you need to do

        _myCounter++;
    }

}

如果您的需求更具体,那么我们的建议就更具体。

使用递归并将当前计数传递到函数中,而不是使用
for
循环:

void checkBoxMethod (object sender, EventArgs args)
{
    CheckBox box = (CheckBox)sender;
    int number = (int)box.Tag;
    myRecursiveMethod(number);
}

private void myRecursiveMethod(int count)
{
    //do whatever stuff I need to do

    if (!timeToExitTheMethod)
        myRecursiveMethod(count++);
}
你没有给我们解释你在
for
循环中做了什么,你的问题没有太多意义(例如,你提到的复选框是什么?),因此我不能非常具体地介绍我的代码示例。请注意,您必须为递归方法编写一个退出点(否则将调用它,直到出现堆栈溢出)

如果您只是想计算函数被调用的次数,请执行以下操作:

public class MyClass
{
    private int _myCounter = 0;

    void checkBoxMethod (object sender, EventArgs args)
    {
        CheckBox box = (CheckBox)sender;

        //do whatever you need to do

        _myCounter++;
    }

}

如果您的要求更具体,那么我们的建议也可以更具体。

您可以使用lambda Expersions将所需信息传递给处理程序

    for (int i = 0; i < n; i++) {
        int number = i;
        buttons[i].Click += (sender, args) => OnButtonClick(sender, args, number);
    }
...
    private void OnButtonClick(object sender, RoutedEventArgs e, int number) {
        MessageBox.Show(number.ToString(CultureInfo.InvariantCulture));
    }
for(int i=0;iOnButtonClick(发件人,参数,编号);
}
...
私有void on按钮单击(对象发送方、路由目标e、整数){
Show(number.ToString(CultureInfo.InvariantCulture));
}

您可以使用lambda expersions将所需信息传递给处理程序

    for (int i = 0; i < n; i++) {
        int number = i;
        buttons[i].Click += (sender, args) => OnButtonClick(sender, args, number);
    }
...
    private void OnButtonClick(object sender, RoutedEventArgs e, int number) {
        MessageBox.Show(number.ToString(CultureInfo.InvariantCulture));
    }
for(int i=0;iOnButtonClick(发件人,参数,编号);
}
...
私有void on按钮单击(对象发送方、路由目标e、整数){
Show(number.ToString(CultureInfo.InvariantCulture));
}

我将如何执行“事件处理程序您可以将发送者变量强制转换为复选框,将复选框的标记属性强制转换为整数”这一部分?对不起,我只需要多一点说明没问题,我在上面解释过。我如何才能做到“事件处理程序您可以将发送者变量转换为复选框,将复选框的标记属性转换为整数”这一部分?对不起,我只是需要多一点说明没问题,我在上面解释了。