VB.NET - Alternative to "Continue For" for Visual Studio 2003

2.3k Views Asked by At

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?

5

There are 5 best solutions below

0
Smur On BEST ANSWER

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
0
Brad Christie On

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
1
Hans Olsson On

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
1
Evgeny Gavrin On

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
0
Meta-Knight On

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