Home All Groups Group Topic Archive Search About

CURRENT INDEX of Iteration on ILIST collection ?

Author
2 Oct 2006 10:57 AM
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

Author
2 Oct 2006 12:40 PM
Brian Gideon
Unfortunately, no.  I find myself wanting such a mechanism an almost
daily basis.

pamelaflue***@libero.it wrote:
Show quoteHide quote
> 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
Author
2 Oct 2006 3:39 PM
Robinson
<pamelaflue***@libero.it> wrote in message
Show quoteHide quote
news:1159786644.169461.107590@h48g2000cwc.googlegroups.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
Author
2 Oct 2006 4:38 PM
pamelafluente
Robinson ha scritto:

Show quoteHide quote
>
> 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


Show quoteHide quote
:)) Pamela
Author
2 Oct 2006 8:39 PM
GhostInAK
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
Author
2 Oct 2006 8:51 PM
pamelafluente
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    `.
                      :         ``.
                     :             `.
                    :  :         .   `.
                    :   :          ` . `.
                     `.. :            `. ``;
                        `:;             `:'
                           :              `.
                            `.              `.     .
                              `'`'`'`---..,___`;..-.  .-.





Show quoteHide quote
>
> -Boo
Author
3 Oct 2006 8:41 AM
Robinson
<pamelaflue***@libero.it> wrote in message
Show quoteHide quote
news:1159822288.058793.308190@c28g2000cwb.googlegroups.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
Author
3 Oct 2006 2:30 PM
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 ...  ;)


Show quoteHide quote
>
>
> 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
Author
2 Oct 2006 9:22 PM
Brian Gideon
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
Author
2 Oct 2006 9:51 PM
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

Show quoteHide quote
> 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
>
Author
3 Oct 2006 12:22 AM
Brian Gideon
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
Author
3 Oct 2006 5:02 AM
Branco Medeiros
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.
Author
3 Oct 2006 5:50 AM
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.

-Boo