CURRENT INDEX of Iteration on ILIST collection ?

P

pamelafluente

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
 
B

Brian Gideon

Unfortunately, no. I find myself wanting such a mechanism an almost
daily basis.
 
R

Robinson

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
 
P

pamelafluente

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
 
G

GhostInAK

Hello pamela,

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
 
P

pamelafluente

GhostInAK ha scritto:
Hello pamela,



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 `.
: ``.
: `.
: : . `.
: : ` . `.
`.. : `. ``;
`:; `:'
: `.
`. `. .
`'`'`'`---..,___`;..-. .-.
 
B

Brian Gideon

GhostInAK said:
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
 
G

GhostInAK

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
 
B

Brian Gideon

GhostInAK said:
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
 
B

Branco Medeiros

GhostInAK said:
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.
 
G

GhostInAK

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.

-Bo
 
R

Robinson

GhostInAK 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:


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
 
P

pamelafluente

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 ... ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top