Q: skipping a For Each

  • Thread starter Thread starter G .Net
  • Start date Start date
G

G .Net

Hi

I was wondering if there is a way to do the following?

If I have a For Each loop, is there a way "jump out" of the current item so
the next item is processed?

For example, I can do it this way

For Each row As DataRow In MyDataTable

If Not row.Item("col1") = "dont do" Then
' Do some stuff
EndIf

Next

however, I'm wondering if there is another way?

Can anyone help?

G
 
G .Net said:
Hi

I was wondering if there is a way to do the following?

If I have a For Each loop, is there a way "jump out" of the current item so
the next item is processed?

For example, I can do it this way

For Each row As DataRow In MyDataTable

If Not row.Item("col1") = "dont do" Then
' Do some stuff
EndIf

Next

however, I'm wondering if there is another way?

Can anyone help?

G

If you are using VB2005, look at the Continue statement.
 
G .Net said:
If I have a For Each loop, is there a way "jump out" of the current item
so the next item is processed?

For example, I can do it this way

For Each row As DataRow In MyDataTable

If Not row.Item("col1") = "dont do" Then
' Do some stuff
EndIf

Next

however, I'm wondering if there is another way?

If you are using VB 2005, check out the 'Continue' statement.
 
Hello G .Net,

Then wrap the code you dont want executed in a conditional.

For ...
DoSomething
If blah = something then
' We dont want this code to execute when blah <> something
End If
Next

-Boo
 
G .Net wrote:
If I have a For Each loop, is there a way "jump out" of the current item so
the next item is processed?
For Each row As DataRow In MyDataTable

If Not row.Item("col1") = "dont do" Then
' Do some stuff
EndIf

Next

however, I'm wondering if there is another way?
<snip>

If you were using VB 8.0 (VS 2005), then you could use the Continue
keyword, which was finally added to the language.

Since, unfortunatelly, you're not, then using If blocks as in your
example is your best approach. You may keep a "Done" flag that would be
tested at the beginning of each block, like this:

For Each Row As DataRow In MyDataTable
Dim Done As Boolean = False '<-- will be false at each cicle
'...
If Not Done AndAlso Not row.Item("col1") = "dont do" Then
' Do some stuff
Done = True
EndIf

If Not Done AndAlso SomeOtherCondition then
'... more stuff
Done = True
EndIf
'...
Next


On the other side, if the logic inside the loop is so complex that
enclosing the code with If blocks would actually hinder its
legibility, consider breaking the method where the loop is located into
simpler methods.

Finally, if that doesn't help either, then you may, as a last resort,
use Goto (no, I'm not trying to start some war, holly or otherwise). To
minimize the ill effects of Goto, ensure that you don't use it to break
out of the loop or jump backwards.

For Each row As DataRow In MyDataTable
'...
If Not row.Item("col1") = "dont do" Then
' Do some stuff
GoTo NextRow
EndIf
'...
NextRow:
Next


Regards,

Branco.
 
Back
Top