开发者

skip a for-loop but from function which is called inside that loop in c#

hey guys m running into a problem, i have a forloop n in that i call a function, and in that function i have a if condition, i want to skip 1 loop if condition gets true, for this problm i was thinkin' to use goto statement but as i read in many forums that goto statement is an evil... can it be solved without using goto statement, ne ideas i dn't want to use '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 =
               开发者_JAVA技巧 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();
}

Actually there are some error list that are accepted errors, so i dn't need to update that in db, So my TextFill(); function shud not run for accepted errors

Snippet Edited


Simple, have the method return a bool. Then you can do:

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


goto won't help you anyway. Basically you can't continue from a different method at all. The only simple way you can keep roughly your current flow is something like this:

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

Note that this will skip the entirety of the next iteration of the loop - which isn't the same as just continue. If you have more code after the call to aFunction and you want to skip that (which is the equivalent of continue) then it's simpler:

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


All you want to do is skip executing the TextFill() function when the condition if (Regex.IsMatch(RechargeText, "Processing") || Regex.IsMatch(RechargeText, "Not")) is true, thats all right?

You can simply return at that if condition, which will work out as you want:

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

now when the above condition works out as true it will return to the for loop and go with the next iteration and so on...

Cheers


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


If your code as posted is exactly how it is, then you could simply return, as the loop has no other operations.


No, it can not be solved with a goto... thankfully.

There is nothing that you can do inside the method that will change the control flow in the loop, you have to return something from the method and use that in the code of the loop:

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


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();
    }
}

You should extract

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

and

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

into its own methods to make this code clearer, looks like a mess.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜