简体   繁体   English

用C#遇到困难

[英]goto difficulties with C#

Why do I get the following compiler error: 为什么会出现以下编译器错误:

//error CS0159: No such label 'lbl_proc_20' //错误CS0159:没有这样的标签“ 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? 为什么我在goto语句上遇到错误以及如何使其生效?

EDIT: Changed the sample to illustrate the actual code. 编辑:更改示例以说明实际的代码。

You can only use goto to jump to labels within the scope of the goto . 您只能使用goto跳转到goto范围内的标签。 From the docs that describes error CS0159 : 从描述错误CS0159文档中:

The label referenced by the goto statement could not be found within the scope of the goto statement. 在goto语句的范围内找不到goto语句引用的标签。

Although the label exists, you can't jump out of an if block into an else block. 尽管标签存在,但是您不能从if块跳到else块。 The code within else is not the same scope as that that contains the goto . else的代码与包含goto

It's time to restructure your code so it doesn't need goto . 现在是重组代码的时候了,因此不需要goto

Edit 编辑

You should try to simplify your logic. 您应该尝试简化逻辑。 Multiple functions are preferable to goto statements. 多个函数比goto语句更可取。

One option you might want to consider is Windows Workflow Foundation . 您可能要考虑的一种选择是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. 然后,WWF将生成处理您指定的逻辑所必需的代码。 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" 作为对“不使用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: C#语言规范(第249页的第8章)指出:

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. 如果当前函数成员中不存在具有给定名称的标签,或者goto语句不在标签范围内,则会发生编译时错误。 This rule permits the use of a goto statement to transfer control out of a nested scope, but not into a nested scope. 该规则允许使用goto语句将控制权移出嵌套范围,但不能移入嵌套范围。

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. 在您的情况下,标签lbl_proc_20goto不在同一个范围内,并且您试图将控制权转移到另一个嵌套范围内。

You can grab the language spec from here: 您可以从这里获取语言规范:

http://www.microsoft.com/en-us/download/details.aspx?id=7029 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