1

为什么会出现以下编译器错误:

//错误 CS0159: 没有这样的标签 'lbl_proc_20'

使用以下代码:

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

为什么我会收到 goto 语句错误以及如何使其工作?

编辑:更改示例以说明实际代码。

4

3 回答 3

10

您只能使用goto跳转到 . 范围内的标签goto。从描述错误的文档CS0159中:

在 goto 语句的范围内找不到 goto 语句引用的标签。

虽然标签存在,但你不能跳出一个if块进入一个else块。其中的代码else与包含goto.

是时候重组你的代码了,这样它就不需要goto.

编辑

您应该尝试简化您的逻辑。多个函数优于goto语句。

您可能要考虑的一种选择是Windows Workflow Foundation。这是一个非常简洁的工具,可让您以流程图的形式直观地表示您的逻辑。然后 WWF 将生成处理您指定的逻辑所需的代码。这可能会起作用,因为看起来您正在创建某种类型的有限状态机或类似过程。

于 2012-09-30T22:08:52.200 回答
2

作为对“如何在没有 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)
    {
于 2012-09-30T23:07:55.900 回答
1

C# 语言规范(第 8 章,第 249 页)指出:

如果当前函数成员中不存在具有给定名称的标签,或者 goto 语句不在标签范围内,则会发生编译时错误。此规则允许使用 goto 语句将控制转移出嵌套范围,但不能转移到嵌套范围内。

在您的情况下,标签lbl_proc_20与 the 不在同一范围内,goto并且您正试图将控制权转移到另一个嵌套范围内。

您可以从这里获取语言规范:

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

于 2012-09-30T23:02:25.370 回答