VB.NET Switch语句GoTo案例

时间:2009-05-04 13:30:19

标签: vb.net switch-statement 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     

但是当我这样做时,我得到一个编译器错误:“Identifier expected”。 “案例”下有一条波浪线。有任何想法吗?

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


我已将代码更改为:

If otherFactor AndAlso parameter = "mvrType" Then 
    'does something here 
Else 
    ' My original "Select Case statement" here without the case for "mvrType" 
End If

10 个答案:

答案 0 :(得分:21)

为什么不这样做:

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

我不确定在最后是否还有其他情况是不是很重要,但是如果你把它放在if的else语句中,似乎你真的不需要去。

答案 1 :(得分:15)

我找不到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;
}

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

Dim CS$4$0000 As String = args(0)
If (Not CS$4$0000 Is Nothing) Then
    If (CS$4$0000 = "UserID") Then
        Console.Write("UserID")
        Return
    End If
    If (CS$4$0000 = "PackageID") Then
        Console.Write("PackageID")
        Return
    End If
    If ((CS$4$0000 = "MVRType") AndAlso (args(1) = "None")) Then
        Console.Write("None")
        Return
    End If
End If
Console.Write("Default")

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

<强>更新

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

的定义

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

答案 2 :(得分:11)

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

答案 3 :(得分:4)

我不确定使用GoTo是个好主意但是如果你想使用它,你可以这样做:

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

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

使用elseif ...

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

使用布尔值...

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

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

答案 4 :(得分:3)

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

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

答案 5 :(得分:2)

你应该先申报标签 用这个:

    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

答案 6 :(得分:1)

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

答案 7 :(得分:0)

现在至少在 Visual Basic 2017 中可以使用一种方法。尽管它不是最漂亮的方法。

GoTo Case "[label]"Goto [Case-label] 仍然不起作用。

标签(此处为 Number2)必须在 Case 之后。这是最令人失望的部分。

dim Value = "1"
Select Case Value
    Case "0"
       ' do nothing, example
    Case "1"
        MsgBox("one")
        GoTo Number2
    Case "2"
Number2:
        MsgBox("two")
    Case "boolean"
        MsgBox("three")
        ' just to show it won't fall through
End Select

答案 8 :(得分:-1)

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

答案 9 :(得分:-1)

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

goto有原因吗?如果它不符合if标准,它就不会执行该功能并转到下一个案例。