Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET Switch Statement GoTo Case

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 
like image 682
KTF Avatar asked May 04 '09 13:05

KTF


People also ask

Can we use Goto in switch case?

A label is a valid C# identifier followed by colon. In this case, case and default statements of a Switch are labels thus they can be targets of a goto. However, the goto statement must be executed from within the switch. that is we cannot use the goto to jump into switch statement .

What is switch case in VB net?

Advertisements. A Select Case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each select case.

How do you write switch statement in VB?

To execute a group of statements depending upon the value of an Expression, then we use the Switch Case. Here, each value is called a Case, and the variable is being switched ON based on each case. Else statement case is executed if the test expression doesn't match with any of the Case specified by the user.

What is for loop in VB net?

A For Next loop is used to repeatedly execute a sequence of code or a block of code until a given condition is satisfied. A For loop is useful in such a case when we know how many times a block of code has to be executed. In VB.NET, the For loop is also known as For Next Loop.


2 Answers

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.

like image 58
ryanulit Avatar answered Sep 22 '22 10:09

ryanulit


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

like image 23
Nick Berardi Avatar answered Sep 25 '22 10:09

Nick Berardi