28

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

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

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


我已将代码更改为如下:

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

10 回答 10

23

为什么不这样做:

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 语句中,您似乎并不需要 go to。

于 2009-05-04T13:42:16.087 回答
16

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

于 2009-05-04T13:46:37.043 回答
12

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

于 2009-05-04T14:02:58.843 回答
7

在 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
于 2012-06-26T19:49:19.003 回答
5

我不确定使用 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 并不是一个好习惯,所以这里有一些替代解决方案:

使用其他...

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

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

于 2009-05-04T13:35:26.803 回答
3

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

    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
于 2009-05-04T13:45:28.713 回答
2
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
于 2009-09-17T03:19:26.007 回答
0
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
于 2012-08-29T09:39:29.193 回答
0
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 标准,它将根本不执行该功能并转到下一个案例。

于 2015-08-13T02:22:52.113 回答
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
于 2021-07-21T18:35:46.640 回答