VB.NET Switch 语句 GoTo 案例
我正在 VB.NET 中编写一些使用 switch 语句的代码,但在其中一种情况下它需要跳转到另一个块。 在 C# 中,它看起来像这样:
switch (parameter)
{
case "userID":
// does something here.
case "packageID":
// does something here.
case "mvrType":
if (otherFactor)
{
// does something here.
}
else
{
goto default;
}
default:
// does some processing...
break;
}
但是,我不知道如何将其转换为 VB.NET。 我尝试了这个:
Select Case parameter
Case "userID"
' does something here.
Case "packageID"
' does something here.
Case "mvrType"
If otherFactor Then
' does something here.
Else
GoTo Case Else
End If
Case Else
' does some processing...
Exit Select
End Select
但是当我这样做时,我收到编译器错误:“需要标识符”。 “Case”下面有一条波浪线。 有任何想法吗?
另外,在这种情况下使用 GoTo 语句是否错误? 看来任何其他方式我都必须重写它。
我已将我的代码更改为如下:
If otherFactor AndAlso parameter = "mvrType" Then
'does something here
Else
' My original "Select Case statement" here without the case for "mvrType"
End If
I am writing some code in VB.NET that uses a switch statement but in one of the cases it needs to jump to another block. In C# it would look like this:
switch (parameter)
{
case "userID":
// does something here.
case "packageID":
// does something here.
case "mvrType":
if (otherFactor)
{
// does something here.
}
else
{
goto default;
}
default:
// does some processing...
break;
}
However, I don't know how to convert this to VB.NET. I tried this:
Select Case parameter
Case "userID"
' does something here.
Case "packageID"
' does something here.
Case "mvrType"
If otherFactor Then
' does something here.
Else
GoTo Case Else
End If
Case Else
' does some processing...
Exit Select
End Select
But when I do this I get a compiler error: "Identifier expected". There'sa squiggly line under "Case". Any ideas?
Also, is it wrong to use a GoTo statement in this case? It seems any other way I would have to re-write it.
I have changed my code to as follows:
If otherFactor AndAlso parameter = "mvrType" Then
'does something here
Else
' My original "Select Case statement" here without the case for "mvrType"
End If
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
你应该先声明标签
用这个 :
you should declare label first
use this :
goto 有理由吗? 如果它不满足 if 条件,它就不会执行该功能并转到下一个情况。
Is there a reason for the goto? If it doesn't meet the if criterion, it will simply not perform the function and go to the next case.
现在有一种方法至少可以在 Visual Basic 2017 中使用。尽管它不是最漂亮的。
GoTo Case "[label]"
和Goto [Case-label]
仍然不起作用。标签(此处为
Number2
)必须位于Case
之后。 这是最令人失望的部分。There's a way that works now at least in Visual Basic 2017. It's not the prettiest though.
GoTo Case "[label]"
andGoto [Case-label]
still do not work.The label (
Number2
here) must be after theCase
. That was the most disappointing part.为什么不这样做:
我不确定最后没有 case else 是否有什么大不了的,但如果你只是把它放在 else 中,你似乎并不真正需要 go to你的如果的陈述。
Why not just do it like this:
I'm not sure if not having a case else at the end is a big deal or not, but it seems like you don't really need the go to if you just put it in the else statement of your if.
我在 VB.NET 中找不到类似的东西。 对于这段代码,您可能希望在 Reflector 中打开它,并将输出类型更改为 VB,以获得所需代码的精确副本。 例如,当我将以下内容放入 Reflector 时:
它给出了以下 VB.NET 输出。
正如您所看到的,您可以使用 If 语句完成相同的 switch case 语句。 通常我不推荐这样做,因为它更难理解,但 VB.NET 似乎不支持相同的功能,并且使用 Reflector 可能是获取所需代码的最佳方法,无需使用很多痛苦。
更新:
刚刚确认您无法在 VB.NET 中执行完全相同的操作,但它确实支持其他一些有用的功能。 看起来 IF 语句转换是您最好的选择,或者可能进行一些重构。 以下是 Select...Case 的定义
http://msdn.microsoft.com /en-us/library/cy37t14y.aspx
There is no equivalent in VB.NET that I could find. For this piece of code you are probably going to want to open it in Reflector and change the output type to VB to get the exact copy of the code that you need. For instance when I put the following in to Reflector:
it gave me the following VB.NET output.
As you can see you can accomplish the same switch case statement with If statements. Usually I don't recommend this because it makes it harder to understand, but VB.NET doesn't seem to support the same functionality, and using Reflector might be the best way to get the code you need to get it working with out a lot of pain.
Update:
Just confirmed you cannot do the exact same thing in VB.NET, but it does support some other useful things. Looks like the IF statement conversion is your best bet, or maybe some refactoring. Here is the definition for Select...Case
http://msdn.microsoft.com/en-us/library/cy37t14y.aspx
为什么不将默认情况重构为方法并从两个地方调用它?
这应该更具可读性,并且允许您稍后以更有效的方式更改代码。
Why don't you just refactor the default case as a method and call it from both places?
This should be more readable and will allow you to change the code later in a more efficient manner.
在 VB.NET 中,即使其他条件不适用于 Select 参数,您也可以应用多个条件。 见下文:
In VB.NET, you can apply multiple conditions even if the other conditions don't apply to the Select parameter. See below:
我不确定使用 GoTo 是个好主意,但如果您确实想使用它,可以执行以下操作:
正如我所说,虽然它有效,但 GoTo 不是一个好的做法,因此这里有一些替代解决方案:
使用 elseif...
使用布尔值...
您还可以在这两种情况下直接调用方法,而不是设置布尔变量...
I'm not sure it's a good idea to use a GoTo but if you do want to use it, you can do something like this:
As I said, although it works, GoTo is not good practice, so here are some alternative solutions:
Using elseif...
Using a boolean value...
Instead of setting a boolean variable you could also call a method directly in both cases...