VB.NET切换语句转到案例 [英] VB.NET Switch Statement GoTo Case

查看:59
本文介绍了VB.NET切换语句转到案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VB.NET中编写一些代码,这些代码使用switch语句,但在某些情况下,它需要跳转到另一个块.在C#中看起来像这样:

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;
}

但是,我不知道如何将其转换为VB.NET.我试过了:

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?

此外,在这种情况下使用GoTo语句是否错误?似乎还有其他方法需要重新编写.

Also, is it wrong to use a GoTo statement in this case? It seems any other way I would have to re-write it.

我将代码更改如下:

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

推荐答案

我找不到VB.NET中的等效项.对于这段代码,您可能要在Reflector中打开它,并将输出类型更改为VB,以获取所需代码的精确副本.例如,当我将以下内容放入Reflector中时:

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;
}

它给了我下面的VB.NET输出.

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

如您所见,您可以使用If语句完成相同的switch case语句.通常我不推荐这样做,因为它使人很难理解,但是VB.NET似乎不支持相同的功能,并且使用Reflector可能是获取所需代码的最佳方法,以使其能够正常工作.很痛苦.

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.

更新:

仅确认您不能在VB.NET中做完全相同的事情,但是它确实支持其他一些有用的事情.看起来IF语句转换是最好的选择,或者可能是一些重构.这是Select ... Case的定义

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

这篇关于VB.NET切换语句转到案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆