For Each

  • Thread starter Thread starter Miro
  • Start date Start date
M

Miro

Is there an "Integer Counter" or a "Loop" number somehow part of the For
each statement?

I find myself doing this a lot ( where I want to omit for example the first
instance )

dim OwnLineWordCounter as integer = 0
For Each OwnLineWord As String In EnterWords
OwnLineWordCounter += 1
If OwnLineWordCounter > 1 Then
'do work here
End If
Next


Sometimes there are certain predefined elements I want to skip over.
Instead of always declaring my own counter for the 'for each', and instead
of using a For Next loop,
does the for each have a built in counter somewhere?

Thanks,

Miro
 
Miro said:
Is there an "Integer Counter" or a "Loop" number somehow part of the For
each statement?

I find myself doing this a lot ( where I want to omit for example the
first instance )

dim OwnLineWordCounter as integer = 0
For Each OwnLineWord As String In EnterWords
OwnLineWordCounter += 1
If OwnLineWordCounter > 1 Then
'do work here
End If
Next


Sometimes there are certain predefined elements I want to skip over.
Instead of always declaring my own counter for the 'for each', and
instead of using a For Next loop,
does the for each have a built in counter somewhere?

If you use .NET 3.5 you might want to look into LINQ which should allow
you e.g.
For Each OwnLineWord As String In EnterWords.Skip(1)
 
Hi,

You could convert your loop to a For/Next instead of For Each/Next.
However... The number of line will be the same, I think, because you have to
assign the object inside the loop.

Dick

--
Richard Grier (Microsoft MVP - Visual Basic) Hard & Software 12962 West
Louisiana Avenue Lakewood, CO 80228 303-986-2179 (voice) Homepage:
www.hardandsoftware.net Author of Visual Basic Programmer's Guide to Serial
Communications, 4th Edition ISBN 1-890422-28-2 (391 pages) published July
2004, Revised July 2006.
 
Miro said:
Is there an "Integer Counter" or a "Loop" number somehow part of the For
each statement?

As far as I know there are no counters in For Each statement because you can
iterate collections which don't always have index numbers.

So if you need a counter, you have to implement it by yourself or use the
normal For loop.

-Teemu
 
Is there an "Integer Counter" or a "Loop" number somehow part of the
For each statement?

I find myself doing this a lot ( where I want to omit for example the
first instance )

dim OwnLineWordCounter as integer = 0
For Each OwnLineWord As String In EnterWords
OwnLineWordCounter += 1
If OwnLineWordCounter > 1 Then
'do work here
End If
Next


Sometimes there are certain predefined elements I want to skip over.
Instead of always declaring my own counter for the 'for each', and
instead of using a For Next loop,
does the for each have a built in counter somewhere?

It depends on the collection you are iterating through. I am not sure
what EnterWords is, as you do not have the definition in your post, so I
cannot tell you. It is possible, with some types of collections, to do
this:

For i As Integer = 0 To (EnterWords.Count - 1)



Next

or skip like so

For i As Integer = 10 To (EnterWords.Count - 1)



Next

In general, the collections that will do this are ordered in some way.

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Thanks,

I was just trying to stick with the 'For Each" statement didnt want to
re-write a whole bunch of loops I had.

I was too lazy to flip them to be For ... Next statements.

I was hoping I would have something like this....

For Each myWord as String In AllMyWords
If Each.counter = 1 Then
'code here
Endif
Next

No worries... I just gotta become 'unlazy'.

Thanks,

Miro
 
Miro,

That is never an argumentation, the answer from Gregory is simply the
answer, there is no other possible.

Your current code is simply dirty and cost only extra time.

(By the way, what you now propose will cost more time and very confusing for
others to maintain)

jmo

Cor
 
Thanks,

I was just trying to stick with the 'For Each" statement didnt want to
re-write a whole bunch of loops I had.

I was too lazy to flip them to be For ... Next statements.

I was hoping I would have something like this....

For Each myWord as String In AllMyWords
If Each.counter = 1 Then
'code here
Endif
Next

No worries... I just gotta become 'unlazy'.


The problem with the code, as you have worked around it, is it is
kludgy. Any time you kludge, you risk a future problem where you code
stops working as expected.

Note I am not saying a kludge might not be a temporary way to solve a
problem if you are going to refactor later. But it sounds like you are
kludging things to avoid refactoring, which is probably asking for a
serious problem down the road. Your mileage may vary.

Peace and Grace,


--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
Is there an "Integer Counter" or a "Loop" number somehow part of the For
each statement?

I find myself doing this a lot ( where I want to omit for example the first
instance )

                dim OwnLineWordCounter as integer = 0
                For Each OwnLineWord As String In EnterWords
                    OwnLineWordCounter += 1
                    If OwnLineWordCounter > 1 Then
                        'do work here
                    End If
                Next

Sometimes there are certain predefined elements I want to skip over.
Instead of always declaring my own counter for the 'for each', and instead
of using a For Next loop,
does the for each have a built in counter somewhere?

Thanks,

Miro

No, unfortunately not. I have been wanting something integrated into
the enumerator/enumerable pattern that does just this for awhile now.
Of course, it's simple to create your own loop variable, but I dislike
that solution because 1) the loop variable has to be scoped outside of
the loop and 2) it lacks that elegant appeal. The problem is I'm not
sure such a feature could be integrated into the language in an
elegant fashion at this point.
 
No, unfortunately not. I have been wanting something integrated into
the enumerator/enumerable pattern that does just this for awhile now.
Of course, it's simple to create your own loop variable, but I dislike
that solution because 1) the loop variable has to be scoped outside of
the loop and 2) it lacks that elegant appeal. The problem is I'm not
sure such a feature could be integrated into the language in an
elegant fashion at this point.

The loop variable no longer has to be scoped outside the loop. I'm
not sure what version introduced this change.

For indx As Integer = 0 To 5
 
No, unfortunately not. I have been wanting something integrated into
the enumerator/enumerable pattern that does just this for awhile now.
Of course, it's simple to create your own loop variable, but I dislike
that solution because 1) the loop variable has to be scoped outside of
the loop and 2) it lacks that elegant appeal. The problem is I'm not
sure such a feature could be integrated into the language in an
elegant fashion at this point.

Not all collections necessarily have an index. So, it seems a little out of
place. If you need an index or loop counter, then a for loop is probably a
better choice - and you do not need to scope the counter ourside of the loop:

Dim a() As Integer = {1, 2, 3, 4, 5}

For i As Integer = 0 To A.Length - 1
Console.WriteLine (a)
Next

' no, no, no! won't compile
Console.WriteLine (i)
 
The loop variable no longer has to be scoped outside the loop.  I'm
not sure what version introduced this change.

For indx As Integer = 0 To 5

A standard For loop cannot be used with all collections. Plus, the
OP's question was specifically related to the For Each loop.

But, to answer your question I think that ability has always been
available in VB.NET.
 
Not all collections necessarily have an index.  So, it seems a little out of
place.  If you need an index or loop counter, then a for loop is probably a
better choice - and you do not need to scope the counter ourside of the loop:

Dim a() As Integer = {1, 2, 3, 4, 5}

For i As Integer = 0 To A.Length - 1
        Console.WriteLine (a)
Next

' no, no, no!  won't compile
Console.WriteLine (i)


Yeah, of course. I was thinking in more general terms of the
collections that don't have an index concept like a hastable or
something.

For some reason I've been using the following pattern *a lot* lately.

Dim a As ICollection(Of String) = GetCollection()
Dim i As Integer = 0
For Each item As String in a
i += 1
Console.WriteLine(item + " is the " + i.ToString() + " item
enumerated.")
Next

It would be neat if there were some way to determine how many times
the loop spun around without needing to declare an additional
variable. But, I'm not sure how badly I want that feature especially
if means adding yet another awkward keyword or element to the language.
 
A standard For loop cannot be used with all collections. Plus, the
OP's question was specifically related to the For Each loop.

But, to answer your question I think that ability has always been
available in VB.NET.

I'm pretty sure that it was added with 2003 - (v7.1).
 
Thanks,

I was just trying to stick with the 'For Each" statement didnt want to
re-write a whole bunch of loops I had.

I was too lazy to flip them to be For ... Next  statements.

I was hoping I would have something like this....

For Each myWord as String In AllMyWords
    If Each.counter = 1 Then
        'code here
    Endif
Next

No worries... I just gotta become 'unlazy'.

Thanks,

I don't think preferring a For Each loop over a standard For loop is
being lazy. I think you actually had a good question. And even
though the feature doesn't exist and probably never will the idea does
have merit.
 
Is there an "Integer Counter" or a "Loop" number somehow part of the For
each statement?

I find myself doing this a lot ( where I want to omit for example the first
instance )

                dim OwnLineWordCounter as integer = 0
                For Each OwnLineWord As String In EnterWords
                    OwnLineWordCounter += 1
                    If OwnLineWordCounter > 1 Then
                        'do work here
                    End If
                Next

Sometimes there are certain predefined elements I want to skip over.
Instead of always declaring my own counter for the 'for each', and instead
of using a For Next loop,
does the for each have a built in counter somewhere?

Thanks,

Miro

I was just thinking that you could create an IEnumerator/IEnumerable
combination that wraps another IEnumerable to provide an elegant way
of doing this.

The end result might look like the following.

For Each item as EnumeratorItem(Of String) in CountingEnumerator.Wrap
(EnterWords)
Console.WriteLine(item.Value)
Console.WriteLine(item.Number)
Next
 
I don't think preferring a For Each loop over a standard For loop is
being lazy. I think you actually had a good question. And even
though the feature doesn't exist and probably never will the idea does
have merit.


The laziness was less an issue than using a kludge inside a for each loop.
As for the question of whether or not one should have a cardinal or ordinal
"pointer" in a for each, I would say the idea is interesting, but of
limited value, as you end up with different answers depending on the
object/collection you use and perhaps the way you order the items put into
the object/collection. As such, it is of limited value and I would
personally advise adding other features over altering the internals, and
possibly slowing them down, for this particular feature.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
As such, it is of limited value and I would
personally advise adding other features over altering the internals, and
possibly slowing them down, for this particular feature.

Absolutely. I totally agree.
 
Back
Top