VB.NET Switch 语句 GoTo 案例

发布于 2024-07-18 02:10:31 字数 1217 浏览 2 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

又怨 2024-07-25 02:10:32

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

    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

you should declare label first
use 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
谷夏 2024-07-25 02:10:32
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
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
差↓一点笑了 2024-07-25 02:10:32
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 条件,它就不会执行该功能并转到下一个情况。

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

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.

殊姿 2024-07-25 02:10:32

现在有一种方法至少可以在 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

There's a way that works now at least in Visual Basic 2017. It's not the prettiest though.

GoTo Case "[label]" and Goto [Case-label] still do not work.

The label (Number2 here) must be after the Case. That was the most disappointing part.

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
千秋岁 2024-07-25 02:10:32
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
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
命硬 2024-07-25 02:10:31

为什么不这样做:

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

我不确定最后没有 case else 是否有什么大不了的,但如果你只是把它放在 else 中,你似乎并不真正需要 go to你的如果的陈述。

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                         
         ' do processing originally part of GoTo here
         Exit Select  
      End If      
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.

嘿看小鸭子会跑 2024-07-25 02:10:31

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

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:

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.

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")

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

携君以终年 2024-07-25 02:10:31

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

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.

无语# 2024-07-25 02:10:31

在 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

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

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
轻许诺言 2024-07-25 02:10:31

我不确定使用 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

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

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:

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

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

Using 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

Using a boolean value...

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文