繁体   English   中英

用C#遇到困难

[英]goto difficulties with C#

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

//错误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语句上遇到错误以及如何使其生效?

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

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

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

尽管标签存在,但是您不能从if块跳到else块。 else的代码与包含goto

现在是重组代码的时候了,因此不需要goto

编辑

您应该尝试简化逻辑。 多个函数比goto语句更可取。

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

作为对“不使用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)
    {

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

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

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

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

http://www.microsoft.com/zh-cn/download/details.aspx?id=7029

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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