简体   繁体   中英

c# Do-While Calling back

I'm stuck in my code!

I have a Do -While Loop and inside I have Switch-Case and each case have If Else Conditions like this :

Here is the problem Once I press the Retry Button(in If-Else Condition ) I want to go back in middle of the Do Loop and rerun it again for checking... I'm not quite sure how I should do this. Any help would be great.

If the switch case is at the bottom of the loop body, you could just call continue and the loop with reiterate from the start.

If you need to start from the middle of the loop, and not the start, the only way I can think of is to wrap the middle in another loop body. Now continue will apply to the inner loop and you will get what you want.

If no other option, you can use goto , but beware the consequences

I really don't know why you'd structure your code like this in a huge loop and switch statement. For me, I'd probably break that up. It would make it simpler to follow. But perhaps you have a reason.

If you can, create an inner loop and continue executing that loop until a non-retry value is returned.

If that really doesn't work, then I'm not above using goto . I know that's taboo for a lot of people but my goal is to write clear code. And if you know what you're doing and a goto results in the clearest code, then I say go for it.

You would probably want to split your code into functions for readability and maintenance piont of view. I would suggest you to try this:

do
{
  if(eErrorCode!=0)
  {
     if( GetResult(eErrorCode) == "Break")
      break;
  }
}

private string GetResult(eErrorCode)
{
  switch(eErrorCode)
  ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM