|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
CURRENT INDEX of Iteration on ILIST collection ?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 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 <pamelaflue***@libero.it> wrote in message
Show quoteHide quote news:1159786644.169461.107590@h48g2000cwc.googlegroups.com... The only way I can think of, that is actually quite idiotic, would be:> 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 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 Robinson ha scritto:
Show quoteHide quote > I agree :))> 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 > This seems a really beautiful idea. Where did you get it ?> > 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 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 Hello pamela,
>> For Each SomeObj As Object In ArrayListObj CountWith i You've got to be joking. A beautiful idea? This from the gal that wanted >> >> Next >> > This seems a really beautiful idea. Where did you get it ? > > I hope Microsoft designer are taking notes :) to count something WITHOUT creating a new variable. -Boo GhostInAK ha scritto:
> Hello pamela, Hi Ghost :))> > > >> 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. 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 <pamelaflue***@libero.it> wrote in message
Show quoteHide quote news:1159822288.058793.308190@c28g2000cwb.googlegroups.com... As I understood the OP, the problem was having to do the plumbing yourself, > > 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. 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 Robinson ha scritto:
> As I understood the OP, the problem was having to do the plumbing yourself, Not only that. Also the convenience to get some variable alredy> 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: 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 GhostInAK wrote:
> You've got to be joking. A beautiful idea? This from the gal that wanted I don't know if I'd call it a beautiful idea, but it isn't exactly> to count something WITHOUT creating a new variable. > > -Boo 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 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 > GhostInAK wrote:
> Hello Brian, Boo,> > Certainly. However the syntax idea provided is no better than the external > variable. (Since when is a programmer afraid to create a variable??) > > -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 GhostInAK wrote:
> Certainly. However the syntax idea provided is no better than the external I think that we already have enough "composed" keywords (WithEvents,> variable. (Since when is a programmer afraid to create a variable??) 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. 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 |
|||||||||||||||||||||||