VB.NET - “继续”的替代方法适用于 Visual Studio 2003

发布于 2024-10-18 17:38:14 字数 257 浏览 6 评论 0原文

我正在尝试跳到 for 循环中的下一个条目。

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 中,这个不存在。我可以使用其他方法吗?

I'm trying to skip to the next entry in a for loop.

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

    ' Do something
Next

In Visual Studio 2008, I can use "Continue For". But in VS Visual Studio 2003, this doesn't exist. Is there an alternative method I could use?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

屌丝范 2024-10-25 17:38:14

如果你的情况属实,你可以什么也不做。

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

Well you could simply do nothing if your condition is true.

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
悲欢浪云 2024-10-25 17:38:14

从我读到的内容来看,VS2003 中不存在继续。但是您可以切换条件,以便仅在不满足条件时执行。

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

Continue, from what I've read, doesn't exist in VS2003. But you can switch your condition around so it only executes when the condition is not met.

For Each i As Item In Items
  If i <> x Then
    ' run code -- facsimile of telling it to continue.
  End If
End For
带上头具痛哭 2024-10-25 17:38:14

它不是那么漂亮,但只是否定了“如果”。

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

    ' Do something
    End If
Next

It's not as pretty, but just negate the If.

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

    ' Do something
    End If
Next
九八野马 2024-10-25 17:38:14

您可以在循环体末尾使用带有标签的 GoTo 语句。

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

You can use GoTo statement with label at the end of cycle body.

For Each i As Item In Items
    If i = x Then GoTo continue
    ' Do somethingNext
    continue:
    Next
清欢 2024-10-25 17:38:14

根据您的代码,可能有点矫枉过正,但这里有一个替代方案:

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

Might be overkill depending on your code but here's an alternative:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文