VB.NET Switch 语句 GoTo 案例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/820104/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 14:12:38  来源:igfitidea点击:

VB.NET Switch Statement GoTo Case

vb.netswitch-statementgoto

提问by KTF

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:

我正在 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;
}

However, I don't know how to convert this to VB.NET. I tried this:

但是,我不知道如何将其转换为 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     

But when I do this I get a compiler error: "Identifier expected". There'sa squiggly line under "Case". Any ideas?

但是当我这样做时,我收到一个编译器错误:“预期标识符”。“Case”下有一条波浪线。有任何想法吗?

Also, is it wrong to use a GoTo statement in this case? It seems any other way I would have to re-write it.

另外,在这种情况下使用 GoTo 语句是错误的吗?似乎任何其他方式我都必须重新编写它。



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

采纳答案by Nick Berardi

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:

在 VB.NET 中没有我能找到的等价物。对于这段代码,您可能希望在 Reflector 中打开它并将输出类型更改为 VB 以获得所需代码的准确副本。例如,当我将以下内容放入 Reflector 时:

switch (args[0])
{
    case "UserID":
        Console.Write("UserID");
        break;
    case "PackageID":
        Console.Write("PackageID");
        break;
    case "MVRType":
        if (args[1] == "None")
            Console.Write("None");
        else
            goto default;
        break;
    default:
        Console.Write("Default");
        break;
}

it gave me the following VB.NET output.

它给了我以下 VB.NET 输出。

Dim CS
Select Case parameter     
   Case "userID"                
      ' does something here.        
   Case "packageID"                
      ' does something here.        
   Case "mvrType"                 
      If otherFactor Then                         
         ' does something here.                 
      Else                         
         ' do processing originally part of GoTo here
         Exit Select  
      End If      
End Select
00 As String = args(0) If (Not CS
Select Case parameter 
    Case "userID"
                ' does something here.
        Case "packageID"
                ' does something here.
        Case "mvrType" And otherFactor
                ' does something here. 
        Case Else 
                ' does some processing... 
End Select
00 Is Nothing) Then If (CS
Select Case parameter 
    Case "userID"
        ' does something here.
    Case "packageID"
        ' does something here.
    Case "mvrType" 
        If otherFactor Then 
            ' does something here. 
        Else 
            GoTo caseElse
        End If 
    Case Else
caseElse:
        ' does some processing... 
End Select
00 = "UserID") Then Console.Write("UserID") Return End If If (CS
If parameter = "userID" Then
    ' does something here.
ElseIf parameter = "packageID" Then
    ' does something here.
ElseIf parameter = "mvrType" AndAlso otherFactor Then
    ' does something here.
Else
    'does some processing...
End If
00 = "PackageID") Then Console.Write("PackageID") Return End If If ((CS
Dim doSomething As Boolean

Select Case parameter
Case "userID"
     ' does something here.
Case "packageID"
     ' does something here.
Case "mvrType"
     If otherFactor Then
          ' does something here. 
     Else
          doSomething = True
     End If
Case Else
     doSomething = True
End Select

If doSomething Then
     ' does some processing... 
End If
00 = "MVRType") AndAlso (args(1) = "None")) Then Console.Write("None") Return End If End If Console.Write("Default")

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.

如您所见,您可以使用 If 语句完成相同的 switch case 语句。通常我不推荐这样做,因为它更难理解,但 VB.NET 似乎不支持相同的功能,并且使用 Reflector 可能是获得所需代码的最佳方式很多痛苦。

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

刚刚确认您不能在 VB.NET 中做完全相同的事情,但它确实支持其他一些有用的东西。看起来 IF 语句转换是您最好的选择,或者可能是一些重构。这是 Select...Case 的定义

http://msdn.microsoft.com/en-us/library/cy37t14y.aspx

http://msdn.microsoft.com/en-us/library/cy37t14y.aspx

回答by ryanulit

Why not just do it like this:

为什么不这样做:

    Select Case parameter 
        Case "userID"
                    ' does something here.
            Case "packageID"
                    ' does something here.
            Case "mvrType" 
                    If otherFactor Then 
                            ' does something here. 
                    Else 
                            GoTo else
                    End If 

            Case Else 
else :
                    ' does some processing... 
                    Exit Select 
    End Select

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.

我不确定最后没有一个案例 else 是否有什么大不了的,但如果你只是把它放在你的 if 的 else 语句中,你似乎真的不需要去。

回答by Nicolas Irisarri

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.

为什么不将默认情况重构为方法并从两个地方调用它?这应该更具可读性,并允许您稍后以更有效的方式更改代码。

回答by Zachafer

In VB.NET, you can apply multiple conditions even if the other conditions don't apply to the Select parameter. See below:

在 VB.NET 中,即使其他条件不适用于 Select 参数,您也可以应用多个条件。见下文:

Select Case parameter 
    ' does something here. 
    ' does something here. 
    Case "userID", "packageID", "mvrType" 
        If otherFactor Then 
            ' does something here. 
        Else 
            goto case default 
        End If 
    Case Else 
        ' does some processing... 
        Exit Select 
End Select

回答by Meta-Knight

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:

我不确定使用 GoTo 是否是个好主意,但如果您确实想使用它,您可以执行以下操作:

Select Case parameter
    ' does something here.
    ' does something here.
    Case "userID", "packageID", "mvrType"
                ' does something here.
        If otherFactor Then
        Else
            goto case default
        End If
    Case Else
        ' does some processing...
        Exit Select
End Select

As I said, although it works, GoTo is not good practice, so here are some alternative solutions:

正如我所说,虽然它有效,但 GoTo 不是好的做法,所以这里有一些替代解决方案:

Using elseif...

使用其他...

Select Case parameter 
    Case "userID"
        ' does something here.
    Case "packageID"
        ' does something here.
    Case "mvrType" 
        If otherFactor Then 
            ' does something here.
        End If 
    Case Else 
        ' does some processing... 
        Exit Select 
End Select

Using a boolean value...

使用布尔值...

##代码##

Instead of setting a boolean variable you could also call a method directly in both cases...

除了设置布尔变量之外,您还可以在两种情况下直接调用方法......

回答by Sadegh

you should declare label first use this :

你应该首先使用这个声明标签:

##代码##

回答by Sadegh

##代码##

回答by Taran

##代码##

回答by Jason Zielke

##代码##

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.

goto有什么原因吗?如果它不满足 if 标准,它将根本不执行该功能并转到下一个案例。