Question

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?

Was it helpful?

Solution

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

OTHER TIPS

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

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

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

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top