跳过一个for循环,但是从c#里面循环调用的函数中跳过

时间:2010-11-06 17:36:21

标签: c#

嘿伙计们遇到一个问题,我有一个forloop n,因为我调用了一个函数,并且在该函数中我有一个if条件,我想跳过1个循环,如果条件变为真,那个问题我想到了'使用goto声明,但正如我在许多论坛中读到goto陈述是邪恶的......可以在不使用goto陈述的情况下解决,我不想要的想法使用'转到'

for(int i=0;i<gridview.rows.count-1;i++)
{
 //some operation;
 aFunction(param1,param2);
}

public void aFunction(param1,param2)
{
 //some operation;
if (!Regex.IsMatch(RechargeText, "successfully"))
        {
            RechargeStatus = "Failed";
            Program.sp.SoundLocation =
                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) +
                "/aimlife_error.wav";
            Program.sp.Play();
        }
        else if (Regex.IsMatch(RechargeText, "Processing") || Regex.IsMatch(RechargeText, "Not"))
        {
            // here i need to skip the Loop
        }
        else
        {
            Program.sp.SoundLocation =
                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) +
                "/aimlife_success.wav";
            Program.sp.Play();
        }
        Program.StatusMessage = "Recharge Successful";
        TextFill();
}

实际上有一些错误列表是可接受的错误,所以我不需要在db中更新它,所以我的TextFill();函数不会因为已接受的错误而运行

代码段编辑

7 个答案:

答案 0 :(得分:4)

简单,让方法返回一个bool。然后你可以这样做:

for(int i=0;i<gridview.rows.count-1;i++)
{
    //some operation;
    if (aFunction(param1,param2)) break;
}

答案 1 :(得分:2)

无论如何,

goto对你无济于事。基本上你不能从一个不同的方法继续。您可以大致保持当前流量的唯一简单方法是:

bool shouldSkipNext = false;
for (int i = 0; i < gridview.Rows.Count - 1; i++)
{
    if (shouldSkipNext)
    {
        shouldSkipNext = false;
        continue;
    }
    // some operation
    shouldSkipNext = aFunction(param1, param2);
}

public bool aFunction(param1,param2)
{
    if (abc)
    {
        return true;
    }
    // Other stuff
    return false;
}

请注意,这将跳过循环的下一次迭代的整个过程 - 这与仅仅continue不同。如果在调用aFunction之后有更多代码,并且您想跳过 (相当于continue),那么它更简单:

for (int i = 0; i < gridview.Rows.Count - 1; i++)
{
    // some operation
    if (aFunction(param1, param2))
    {
        continue;
    }
    // Other stuff which will sometimes be skipped
}

答案 2 :(得分:2)

当条件if (Regex.IsMatch(RechargeText, "Processing") || Regex.IsMatch(RechargeText, "Not"))为真时,你想要做的就是跳过执行TextFill()函数,那好吧?

你可以简单地返回if if条件,这将按你的要求运行:

else if (Regex.IsMatch(RechargeText, "Processing") || Regex.IsMatch(RechargeText, "Not"))
    {
          return;
   } .... // rest of the code as it is

现在当上述条件为true时,它将返回到for循环并继续下一次迭代,依此类推......

干杯

答案 3 :(得分:1)

for(int i=0;i<gridview.rows.count-1;i++)
{
 //some operation;
 if (!aFunction(param1,param2)) continue;
}

public bool aFunction(param1,param2)
{
 //some operation;
if (!Regex.IsMatch(RechargeText, "successfully"))
        {
            RechargeStatus = "Failed";
            Program.sp.SoundLocation =
                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) +
                "/aimlife_error.wav";
            Program.sp.Play();
        }
        else if (Regex.IsMatch(RechargeText, "Processing") || Regex.IsMatch(RechargeText, "Not"))
        {
            Program.StatusMessage = "Recharge Successful";
            TextFill();
            return false;
            // here i need to skip the Loop
        }
        else
        {
            Program.sp.SoundLocation =
                System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) +
                "/aimlife_success.wav";
            Program.sp.Play();
        }
        Program.StatusMessage = "Recharge Successful";
        TextFill();

        return true;
}

答案 4 :(得分:0)

如果您发布的代码完全是这样的,那么您可以简单地返回,因为循环没有其他操作。

答案 5 :(得分:0)

不,用goto无法解决...谢天谢地。

在方法内部没有任何东西可以改变循环中的控制流,你必须从方法返回一些东西并在循环代码中使用它:

for(int i = 0; i < gridview.rows.count - 1; i++) {
  //some operation;
  if (aFunction(param1,param2)) {
    // do something more here
  }
}

public bool aFunction(param1,param2) {
  //some operation;
  if(abc) {
    //skip the current for-loop i.e i want to do "continue" here;
    return false;
  } else {
    //normal
    return true;
  }
}

答案 6 :(得分:0)

public static void main(String[] args) {

    int param1 = 0, param2 = 0;
    getResult(param1, param2);
}

public static void getResult(Object param1, Object param2) {
    for (int i = 0; i < gridview.rows.count - 1; i++) {
        if (!Regex.IsMatch(RechargeText, "successfully")) {
            RechargeStatus = "Failed";
            Program.sp.SoundLocation = System.IO.Path
                    .GetDirectoryName(System.Reflection.Assembly
                            .GetExecutingAssembly().Location)
                    + "/aimlife_error.wav";
            Program.sp.Play();
        } else if (Regex.IsMatch(RechargeText, "Processing")
                || Regex.IsMatch(RechargeText, "Not")) {
            // just skip here then
            continue;
        } else {
            Program.sp.SoundLocation = System.IO.Path
                    .GetDirectoryName(System.Reflection.Assembly
                            .GetExecutingAssembly().Location)
                    + "/aimlife_success.wav";
            Program.sp.Play();
        }
        Program.StatusMessage = "Recharge Successful";
        TextFill();
    }
}

你应该提取

Program.sp.SoundLocation = System.IO.Path
                    .GetDirectoryName(System.Reflection.Assembly
                            .GetExecutingAssembly().Location)
                    + "/aimlife_success.wav";
            Program.sp.Play();

 RechargeStatus = "Failed";
            Program.sp.SoundLocation = System.IO.Path
                    .GetDirectoryName(System.Reflection.Assembly
                            .GetExecutingAssembly().Location)
                    + "/aimlife_error.wav";
            Program.sp.Play();

进入自己的方法使这段代码更清晰,看起来就像一团糟。