VB.NET Switch 语句 GoTo 案例 [英] VB.NET Switch Statement GoTo Case

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

问题描述

我正在 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 Switch 语句 GoTo 案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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