-3

How can I stop using the goto here:

if(true)
{
    goto ln1;
}
DialogResult b=DialogResult.Yes;
while(b==DialogResult.Yes){
    //Stuff
    ln1: Function();  
}

Because using a goto from one scope to another is clearly not allowed.(It gives an error). So what can I do instead? I need to get to the SECOND line of code, not first.

4 Answers4

3

If you want to enter the loop without an initial check of the condition, use a do ... while loop.

Example:

DialogResult b;
do {
   b = ShowDialog(myYesNoDialog);
} while(b == DialogResult.Yes);

If you want to use the initial check or not depending on a condition, use a boolean variable:

bool skipCondition = (some condition);
DialogResult b = DialogResult.Yes;
while (skipCondition || b == DialogResult.Yes) {
   skipCondition = false;
   b = ShowDialog(myYesNoDialog);
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    @EdwardKarak: If you want to enter the loop without the initial check, then you don't need anything instead of `goto`, the `do ... while` loop does exactly that. If you want to use the initial check or not depending on a condition, I added an alternative for that above. – Guffa Nov 23 '13 at 23:58
1

Do not use goto label; goto makes your code very complicated! I think you have to rethink in your classes design and your function workflow.
Very helpful link: 'Goto' is this bad?

var runFlag = false;
   if (true)
   {
       runFlag = true;
   }
   DialogResult b = DialogResult.Yes;
   while (b == DialogResult.Yes || runFlag) 
   {
     Function(); 
   }
Community
  • 1
  • 1
Bassam Alugili
  • 16,345
  • 7
  • 52
  • 70
  • 1
    Goto has nothing to break "OOP rules" - goto is a flow control construct. OOP is a coding paradigm. The two are really unrelated. The reason to avoid `goto` is because it's confusing to read code with `goto`. See http://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF – Benjamin Gruenbaum Nov 23 '13 at 23:36
  • Well, sometimes it seems, `goto` is more readable than `break`. – JeffRSon Nov 23 '13 at 23:38
  • @User3805967 the paper I linked you to __IS__ Dijkstra's paper (maybe you should consider _reading_ it?). OOP talks about objects, their interactions and how program communication should happen. `goto` is a flow control construct, it's a part of the syntax, and has nothing to do with good or bad OOP. Claiming it does indicates lack of basic understanding of what a coding paradigm is, and what language syntax is (and how the two are connected in this case). Consider revising your answer to make sense. – Benjamin Gruenbaum Nov 23 '13 at 23:47
  • Maybe then my English description was really confusing. because we are agree with your last words. – Bassam Alugili Nov 23 '13 at 23:51
  • Ah great! Then please consider revising your answer and removing the irrelevant parts about "breaking OOP". – Benjamin Gruenbaum Nov 23 '13 at 23:51
0

It seems to be something like that:

DialogResult b=...; // must be initialized - it's not correct to skip that

if(true)
{
    // goto ln1;
} else {
    b=DialogResult.Yes;
}
do
{
  /*ln1:*/ Function();
}
while(b==DialogResult.Yes); 
JeffRSon
  • 10,404
  • 4
  • 26
  • 51
0

You do realise that your code skips a lot of lines so the program won't actually compile let lone run? What you have written is the same as

    do
{
    Function();
}while(b==DialogResult.Yes)

To make it work you'd need to execute

DialogResult b = DialogResult.Yes;

first

LoztInSpace
  • 5,584
  • 1
  • 15
  • 27