2011-02-24 83 views
3

我試圖跳到for循環中的下一個條目。VB.NET - 替代Visual Studio 2003的「繼續」

For Each i As Item In Items 
    If i = x Then 
     Continue For 
    End If 

    ' Do something 
Next 

在Visual Studio中  2008年 ,我可以用 「持續」。但是在VS Visual   Studio   2003中,這不存在。有沒有其他方法可以使用?

回答

4

那麼,如果你的條件是真的,你可以不做任何事情。

For Each i As Item in Items 
    If i <> x Then ' If this is FALSE I want it to continue the for loop 
     ' Do what I want where 
    'Else 
     ' Do nothing 
    End If 
Next 
+0

適合我的例子。謝謝。 – Urbycoz 2011-02-24 15:02:03

+0

@Urbycoz沒問題! – Smur 2011-02-24 17:24:09

2

繼續,從我讀過的,在VS2003中不存在。但是,您可以切換條件,以便只在條件不滿足時才執行。

For Each i As Item In Items 
    If i <> x Then 
    ' run code -- facsimile of telling it to continue. 
    End If 
End For 
1

這不是很漂亮,只是否定了If。

For Each i As Item In Items 
    If Not i = x Then 

    ' Do something 
    End If 
Next 
+0

爲什麼不使用[operator <>](https://docs.microsoft.com/zh-cn/dotnet/visual-basic/programming-guide/language-features/operators-and-expressions/comparison-operators)? – 2017-06-01 18:17:52

1

您可以在循環體的末尾使用帶有標籤的GoTo語句。

 
For Each i As Item In Items 
    If i = x Then GoTo continue 
    ' Do somethingNext 
    continue: 
    Next 
+0

我知道「goto」語句不受歡迎,但實際上這似乎更像是其他任何其他語言的通用解決方案。 – Urbycoz 2011-02-24 15:05:08

0

可能是矯枉過正,這取決於你的代碼,但在這裏是一種替代方案:

For Each i As Item In Items 
    DoSomethingWithItem(i) 
Next 

... 

Public Sub DoSomethingWithItem(i As Item) 
    If i = x Then Exit Sub 
    'Code goes here 
End Sub