2011-02-24 78 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