代码之家  ›  专栏  ›  技术社区  ›  FosterZ

跳过for循环,但从在c循环中调用的函数#

c#
  •  0
  • FosterZ  · 技术社区  · 14 年前

    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(); 函数shud不为可接受的错误运行

    编辑的代码段

    7 回复  |  直到 14 年前
        1
  •  2
  •   Jon Skeet    14 年前

    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 你想跳过 那个 持续 )那就更简单了:

    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
  •  4
  •   Hans Passant    14 年前

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

    for(int i=0;i<gridview.rows.count-1;i++)
    {
        //some operation;
        if (aFunction(param1,param2)) break;
    }
    
        3
  •  2
  •   user474407 user474407    14 年前

    您只需要在条件 if (Regex.IsMatch(RechargeText, "Processing") || Regex.IsMatch(RechargeText, "Not")) 是真的,没关系吧?

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

    当上述条件被证明为 true 它将返回for循环并继续下一个迭代,依此类推。。。

    干杯

        4
  •  1
  •   Saeed Amiri    14 年前
    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;
    }
    
        5
  •  0
  •   Puppy    14 年前

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

        6
  •  0
  •   Guffa    14 年前

    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;
      }
    }
    
        7
  •  0
  •   atamanroman    14 年前
    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();
    

    用自己的方法使代码更清晰,看起来一团糟。