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
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:
it gave me the following VB.NET output.
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
Why not just do it like this:
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.
you should declare label first use this :