By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,469 Members | 1,259 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,469 IT Pros & Developers. It's quick & easy.

CURRENT INDEX of Iteration on ILIST collection ?

P: n/a
Hi guys,

Is it possible to get the current index, when iterating with FOR EACH
on some collection
(implementing ILIST) without using and external counting variable (,
that
is somehow getting the internal enumerator) ?
For Each SomeObj As Object In ArrayListObj

dim CurrentIndex as integer = ?????? (0,...,
ArrayListObj.count - 1 )

Next SomeObj
-P

Oct 2 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Unfortunately, no. I find myself wanting such a mechanism an almost
daily basis.

pa***********@libero.it wrote:
Hi guys,

Is it possible to get the current index, when iterating with FOR EACH
on some collection
(implementing ILIST) without using and external counting variable (,
that
is somehow getting the internal enumerator) ?
For Each SomeObj As Object In ArrayListObj

dim CurrentIndex as integer = ?????? (0,...,
ArrayListObj.count - 1 )

Next SomeObj
-P
Oct 2 '06 #2

P: n/a

<pa***********@libero.itwrote in message
news:11**********************@h48g2000cwc.googlegr oups.com...
Hi guys,

Is it possible to get the current index, when iterating with FOR EACH
on some collection
(implementing ILIST) without using and external counting variable (,
that
is somehow getting the internal enumerator) ?
For Each SomeObj As Object In ArrayListObj

dim CurrentIndex as integer = ?????? (0,...,
ArrayListObj.count - 1 )

Next SomeObj

The only way I can think of, that is actually quite idiotic, would be:
For Each SomeObj As Object In ArrayListObj

Dim CurrentIndex As Integer = ArrayListObj.IndexOf ( SomeObj )

Next
It's idiotic because it would be so much more efficient to have an
incrementing variable. My preferred solution is to always code loops like
this where I need to index:
For i As Integer = 0 To ArrayListObj.Count - 1

Dim SomeObj As Object = ArrayListObj ( i )

Next
I'm not sure how the language would work out with a special construct for
doing this, such as a "CountWith i" statement...We will leave it up to the
language theorists in the group ;)

For Each SomeObj As Object In ArrayListObj CountWith i

Next


Oct 2 '06 #3

P: n/a

Robinson ha scritto:
>
The only way I can think of, that is actually quite idiotic, would be:
For Each SomeObj As Object In ArrayListObj

Dim CurrentIndex As Integer = ArrayListObj.IndexOf ( SomeObj )

Next
It's idiotic because it would be so much more efficient to have an
incrementing variable. My preferred solution is to always code loops like
this where I need to index:
>

For i As Integer = 0 To ArrayListObj.Count - 1

Dim SomeObj As Object = ArrayListObj ( i )

Next

I agree :))
>

I'm not sure how the language would work out with a special construct for
doing this, such as a "CountWith i" statement...We will leave it up to the
language theorists in the group ;)

For Each SomeObj As Object In ArrayListObj CountWith i

Next

This seems a really beautiful idea. Where did you get it ?

I hope Microsoft designer are taking notes :)

-P
ps.

perhaps also:

For Each SomeObj As Object In ArrayListObj UseIndex I as integer

Next SomeObj

or

For Each SomeObj As Object In ArrayListObj UseIndex I as integer
StartingFrom 0

Next SomeObj
:)) Pamela

Oct 2 '06 #4

P: n/a
Hello pamela,

>For Each SomeObj As Object In ArrayListObj CountWith i

Next
This seems a really beautiful idea. Where did you get it ?

I hope Microsoft designer are taking notes :)
You've got to be joking. A beautiful idea? This from the gal that wanted
to count something WITHOUT creating a new variable.

-Boo
Oct 2 '06 #5

P: n/a

GhostInAK ha scritto:
Hello pamela,

For Each SomeObj As Object In ArrayListObj CountWith i

Next
This seems a really beautiful idea. Where did you get it ?

I hope Microsoft designer are taking notes :)

You've got to be joking. A beautiful idea? This from the gal that wanted
to count something WITHOUT creating a new variable.
Hi Ghost :))

of course it would be understood that the count variable would be an
"alias" for an "internal" variable, ... at least this is the way I
thought I understood it :)

boo
.-.
.' `.
.-. :g g :
: o `.
: ``.
: `.
: : . `.
: : ` . `.
`.. : `. ``;
`:; `:'
: `.
`. `. .
`'`'`'`---..,___`;..-. .-.


>
-Boo
Oct 2 '06 #6

P: n/a

GhostInAK wrote:
You've got to be joking. A beautiful idea? This from the gal that wanted
to count something WITHOUT creating a new variable.

-Boo
I don't know if I'd call it a beautiful idea, but it isn't exactly
absurb either. Afterall, the compiler is already generating the code
to hook in the enumerator for you. It could theorectically generate a
few more IL instructions to provide an iteration count.

Brian

Oct 2 '06 #7

P: n/a
Hello Brian,

Certainly. However the syntax idea provided is no better than the external
variable. (Since when is a programmer afraid to create a variable??)

-Boo
GhostInAK wrote:
>You've got to be joking. A beautiful idea? This from the gal that
wanted to count something WITHOUT creating a new variable.

-Boo
I don't know if I'd call it a beautiful idea, but it isn't exactly
absurb either. Afterall, the compiler is already generating the code
to hook in the enumerator for you. It could theorectically generate a
few more IL instructions to provide an iteration count.

Brian

Oct 2 '06 #8

P: n/a

GhostInAK wrote:
Hello Brian,

Certainly. However the syntax idea provided is no better than the external
variable. (Since when is a programmer afraid to create a variable??)

-Boo
Boo,

I'm certainly not advocating the proposed syntax, but it would be
better in that the scope of the variable is tighter. I'm just not sure
what other problems may exist or if it's worth adding another keyword
to the language.

Brian

Oct 3 '06 #9

P: n/a
GhostInAK wrote:
Certainly. However the syntax idea provided is no better than the external
variable. (Since when is a programmer afraid to create a variable??)
I think that we already have enough "composed" keywords (WithEvents,
AddressOf, AddHandler, AndAlso, MustInherit, etc).

But one time or another I keep forgetting the mandatory Index += 1 when
I have to count inside loops, so yes, an alternate syntax would be
welcome. But nothing similar to "CountWith", I hope...

On the other hand, if the OP doesn't mind counting from 1 instead of
0, she might dare to use the following:

For Each C As Char In "abcdefg"
Dim Index As Integer = Index + 1
Debug.Print("{0} -{1}", Index, C)
Next

Since everything is zero-based in .Net I doubt this has any practical
use, but then, again...
HTH.

Regards.

Branco.

Oct 3 '06 #10

P: n/a
Not nessecarily in response to Branco.. but what exactly is wrong with the
provided mecahnism??

Dim tCount as Integer = 0
Dim tItem as Whatever = Nothing

' This:
For Each tItem In SomeBigArray
' Do crap
tCount = tCount +1
Next

' Or this:
For tCount = 0 to someBigArray.Length - 1
tItem = SomeBigArray(tCount)
Next

The syntax and mechanism for both is perfectly fine. Stop whining and start
coding.

-Boo
Oct 3 '06 #11

P: n/a

<pa***********@libero.itwrote in message
news:11**********************@c28g2000cwb.googlegr oups.com...
>
GhostInAK ha scritto:
>Hello pamela,

>For Each SomeObj As Object In ArrayListObj CountWith i

Next

This seems a really beautiful idea. Where did you get it ?

I hope Microsoft designer are taking notes :)

You've got to be joking. A beautiful idea? This from the gal that
wanted
to count something WITHOUT creating a new variable.

As I understood the OP, the problem was having to do the plumbing yourself,
not the creation of a variable - a shortcut would be preferred. My idea
provides a simple shortcut and is potentially less error prone (the
forgotten increment for example, or the integer in-scope). "i" is in the
scope of the loop, i is auto-incremented. Being in scope would remove many
errors due to not initializing the counter correctly, for example:
Dim i As Integer = 0

For Each SomeObj As Object In ArrayListObj

i += 1

Next

' Whoops, forgot to reset i to zero.........

For Each SomeObj As Object In ArrayListObj2

i += 1

Next
Oct 3 '06 #12

P: n/a

Robinson ha scritto:

As I understood the OP, the problem was having to do the plumbing yourself,
not the creation of a variable - a shortcut would be preferred. My idea
provides a simple shortcut and is potentially less error prone (the
forgotten increment for example, or the integer in-scope). "i" is in the
scope of the loop, i is auto-incremented. Being in scope would remove many
errors due to not initializing the counter correctly, for example:
Not only that. Also the convenience to get some variable alredy
defined internally
For Each SomeObj As Object In ArrayListObj

msgbox( CurrentEnumerator.CurrentIndex )

Next SomeObj

Of course, nothing crucial. But even "For Each" wasn't ... ;)

>

Dim i As Integer = 0

For Each SomeObj As Object In ArrayListObj

i += 1

Next

' Whoops, forgot to reset i to zero.........

For Each SomeObj As Object In ArrayListObj2

i += 1

Next
Oct 3 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.