goto difficulties with C#

喜夏-厌秋 提交于 2019-12-11 11:55:00

问题


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.


回答1:


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.




回答2:


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)
    {



回答3:


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



来源:https://stackoverflow.com/questions/12665530/goto-difficulties-with-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!