使用C#遇到困难

编程入门 行业动态 更新时间:2024-10-24 14:26:19
本文介绍了使用C#遇到困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

Why do I get the following compiler error:

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

//error CS0159: No such label '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语句出错并且如何使其起作用?

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

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

Changed the sample to illustrate the actual code.

推荐答案

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

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

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

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

尽管标签存在,但是您不能从if块跳入else块. else中的代码与包含goto的代码的作用域不同.

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.

是时候重构代码了,所以不需要goto.

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

修改

您应该尝试简化逻辑.多个功能优于goto语句.

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

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

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.

更多推荐

使用C#遇到困难

本文发布于:2023-11-27 11:59:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1637976.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:遇到困难

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!