我正在VB.NET中編寫一些使用switch語句的代碼,但在其中一種情況下需要跳轉到另一個塊。在C#中它看起來像這樣:VB.NET Switch Statement GoTo案例
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
但是,當我這樣做時,我得到一個編譯器錯誤:「標識符預期」。 「Case」下有一條曲線。有任何想法嗎?
另外,在這種情況下使用GoTo語句是錯誤的嗎?這似乎有其他方式我不得不重新寫它。
我已經改變了我的代碼如下:
If otherFactor AndAlso parameter = "mvrType" Then
'does something here
Else
' My original "Select Case statement" here without the case for "mvrType"
End If
您錯過了開關機箱特定GoTo的要點,它不像GoTo那樣工作,只是程序跳轉到另一個案例。 – 2009-05-04 13:38:27
是的,Nick Berardi在談論什麼,正是我需要的。在VB.NET中是否有類似的東西? – KTF 2009-05-04 13:40:42
這就是GoTo在VB.NET下的工作方式 – jgallant 2009-05-04 13:43:49