VB.NET开关语句GoTo案例
发布时间:2020-12-16 23:53:36 所属栏目:大数据 来源:网络整理
导读:我在VB.NET中编写一些使用switch语句的代码,但是在需要跳转到另一个块的情况之一。在C#中,它将如下所示: switch (parameter) { case "userID": // does something here. case "packageID": // does something here. case "mvrType": if (otherFactor) { /
我在VB.NET中编写一些使用switch语句的代码,但是在需要跳转到另一个块的情况之一。在C#中,它将如下所示:
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。我试过这个: 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 但是当我这样做,我得到一个编译器错误:“标识符预期”。 “案例”下面有一条波浪线。有任何想法吗? 此外,在这种情况下使用GoTo语句是错误的吗?似乎任何其他方式我都不得不重写。 我已经将我的代码改为如下: 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:
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输出。 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可能是获得需要的代码以使其正常工作的最佳方法很痛苦 更新: 只是确认你不能在VB.NET中做同样的事情,但它确实支持一些其他有用的东西。看来IF语句的转换是你最好的选择,也可能是一些重构。这是Select … Case的定义 http://msdn.microsoft.com/en-us/library/cy37t14y.aspx (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |