Home All Groups Group Topic Archive Search About

Q: skipping a For Each

Author
6 Jul 2006 3:33 PM
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

Author
6 Jul 2006 3:38 PM
Al Reid
Show quote Hide quote
"G .Net" <nodamnspam@email.com> wrote in message news:ZtKdndqqRtuusDDZRVnyvw@pipex.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

If you are using VB2005, look at the Continue statement.

--
Al Reid
Author
6 Jul 2006 3:38 PM
Herfried K. Wagner [MVP]
Show quote Hide quote
"G .Net" <nodamnspam@email.com> schrieb:
> 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.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
6 Jul 2006 4:24 PM
G .Net
Ah, unfortunately, I'm using VS2003

G

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:OuwKGJRoGHA.1204@TK2MSFTNGP04.phx.gbl...
> "G .Net" <nodamnspam@email.com> schrieb:
>> 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.
>
> --
> M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
> V B   <URL:http://classicvb.org/petition/>
Author
7 Jul 2006 8:39 PM
GhostInAK
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
Author
11 Jul 2006 2:14 AM
Branco Medeiros
G .Net wrote:
<snip>
> If I have a For Each loop, is there a way "jump out" of the current item so
> the next item is processed?
<snip>
> 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.