VB.NET Switch Statement GoTo Case

前端 未结 9 994
你的背包
你的背包 2020-12-10 23:42

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:

switc         


        
相关标签:
9条回答
  • 2020-12-11 00:19

    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
    
    0 讨论(0)
  • 2020-12-11 00:22

    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
    
    0 讨论(0)
  • 2020-12-11 00:22
    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
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-11 00:26

    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...

    0 讨论(0)
  • 2020-12-11 00:27

    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.

    0 讨论(0)
提交回复
热议问题