简体   繁体   中英

goto difficulties with C#

Why do I get the following compiler error:

//error CS0159: No such label 'lbl_proc_20'

with the following code:

//JUST A DUMMY CODE TO ILLUSTRATE THE CONCEPT
int a = resultOfFunction1();
int b = resultOfFunction2();

//10+ Local variables that are calculated depending on the results above

if (a < 10)
{
    switch (b)
    {
        case 0:
            //Actions for A<10, B=0, using local variables
            break;
        case 1:
            double c = someFunction(a, b);  //In real code involves calculations based on a and b
            if(c > 10.0)
                goto lbl_proc_20;   //error CS0159: No such label 'lbl_proc_20' within the scope of the goto statement

                    //Actions for A<10, B=1, using local variables
            break;
        default:
            //Actions for A<10, B=Other, using local variables
            break;
    }
}
else if (a < 20)
{
lbl_proc_20:
    switch(b)
    {
        case 0:
            //Actions for A<20, B=0, using local variables
            break;
        case 1:
            //Actions for A<20, B=1, using local variables
            break;
        case 2:
            //Actions for A<20, B=2, using local variables
            break;
        default:
            //Actions for A<20, B=Other, using local variables
            break;
    }
}
else if (a < 30)
{
    switch(b)
    {
        case 0:
            //Actions for A<30, B=0, using local variables
            break;
        case 1:
            //Actions for A<30, B=1, using local variables
            break;
        case 2:
            //Actions for A<30, B=2, using local variables
            break;
        default:
            //Actions for A<30, B=Other, using local variables
            break;
    }
}

Why am I getting an error for a goto statement and how to make it work?

EDIT: Changed the sample to illustrate the actual code.

You can only use goto to jump to labels within the scope of the goto . From the docs that describes error CS0159 :

The label referenced by the goto statement could not be found within the scope of the goto statement.

Although the label exists, you can't jump out of an if block into an else block. The code within else is not the same scope as that that contains the goto .

It's time to restructure your code so it doesn't need goto .

Edit

You should try to simplify your logic. Multiple functions are preferable to goto statements.

One option you might want to consider is Windows Workflow Foundation . It's a really neat tool that lets you represent your logic visually as a flow chart. WWF will then generate the code necessary to handle the logic you specify. This might work for since it appears like you are creating some type of finite state machine or similar process.

As a response to "How to do it without goto"

bool pretendA20 = false;

if (a < 10)
{
    switch (b)
    {
        case 0:
            //Actions for A<10, B=0, using local variables
            break;
        case 1:
            double c = someFunction(a, b);  //In real code involves calculations based on a and b
            if(c > 10.0)
            {
                //goto lbl_proc_20;   
                pretendA20 = true;
                break;
             }

             //Actions for A<10, B=1, using local variables

            break;
        default:
            //Actions for A<10, B=Other, using local variables
            break;
    }
}

if ((a >= 10 && a < 20) || pretendA20)
{
//lbl_proc_20:
    switch(b)
    {

The C# language specification (chapter 8 on page 249) states:

If a label with the given name does not exist in the current function member, or if the goto statement is not within the scope of the label, a compile-time error occurs. This rule permits the use of a goto statement to transfer control out of a nested scope, but not into a nested scope.

In your case the label lbl_proc_20 is not in the same scope as the goto and you're trying to transfer control into another nested scope.

You can grab the language spec from here:

http://www.microsoft.com/en-us/download/details.aspx?id=7029

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