VB.NET - How to go to the next element a For each loop?

Is there a status like Exit For , but instead of exiting the loop, it just moves on to the next element.

For example:

 For Each I As Item In Items If I = x Then ' Move to next item End If ' Do something Next 

I know it can just add Else to the If statement so that it reads the following:

 For Each I As Item In Items If I = x Then ' Move to next item Else ' Do something End If Next 

Just wondering if there is a way to go to the next item in the Items list. I'm sure most of them will ask correctly, why not just use the Else operator, but for me, wrapping the "Do Something" code seems less readable. Especially when there is a lot more code.

+70
loops
May 6 '09 at 1:53 pm
source share
5 answers
 For Each I As Item In Items If I = x Then Continue For ' Do something Next 
+136
May 6 '09 at 1:56 p.m.
source share

Instead, I would use the Continue statement:

 For Each I As Item In Items If I = x Then Continue For End If ' Do something Next 

Note that this is slightly different than moving the iterator itself - nothing before If is executed again. This is usually what you want, but if not, you will have to use GetEnumerator() and then MoveNext() / Current explicitly, rather than using a For Each loop.

+37
May 6 '09 at 1:56 pm
source share

What about:

 If Not I = x Then ' Do something ' End If ' Move to next item ' 
+3
May 6 '09 at 13:57
source share

I want to be clear that the following code is not good practice. You can use the goto label:

 For Each I As Item In Items If I = x Then 'Move to next item GOTO Label1 End If ' Do something Label1: Next 
+3
May 6 '09 at
source share

When I tried Continue For , it worked, I got a compiler error. When doing this, I found Resume:

 For Each I As Item In Items If I = x Then 'Move to next item Resume Next End If 'Do something Next 

Note: here I am using VBA.

+1
Sep 28 '11 at 11:28
source share



All Articles