tags:

views:

467

answers:

7

I'm using the following syntax to loop through a list collection:

For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors

    i = IndexOf(PropertyActor)
Next

How do I get the index of the current object within the loop? I'm using IndexOf(PropertyActor) but this seems inefficient as it searches the collection when I already have the object available!

A: 

just initialize an integer variable before entering the loop and iterate it...

Dim i as Integer 
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
    i++
Next
sebastian
As Joel implied, the For Each block uses the IEnumerable interface which doesn't necessarily use the same order as an index.
harley.333
I still believe that that the 'i' variable will give you the correct answer...
sebastian
A: 

Add an index variable that you increase yourself for each iteration?

divideandconquer.se
yes, as I did in the code I posted
sebastian
+9  A: 

AFAIK since this pulls the object out of the collection, you would have to go back to the collection to find it.

If you need the index, rather than using a for each loop, I would just use a for loop that went through the indices so you know what you have.

Mitchel Sellers
This approach would suffer the same drawback, as collections are hashlists, and accessing by index is as bad as using indexof.Use a separate counter, you can afford that integer.
tabdamage
That may be, but personally if you need the index, a for loop make much more practical sense, and isn't something that is going to look out of place.
Mitchel Sellers
+2  A: 

It might be easiest to just keep a separate counter:

i = 0
For Each PropertyActor As JCPropertyActor In MyProperty.PropertyActors
    ...
    i = i + 1
Next

As an aside, Python has a convenient way of doing this:

for i, x in enumerate(a):
    print "object at index ", i, " is ", x
Greg Hewgill
I don't understand why is this so different to what I wrote... you get +2 and I got -2 :(
sebastian
The index you create/maintain won't necessarily be the same index the collection has (because foreach doesn't necessarily iterate in index order). This will also likely get rated down with the other, similar, wrong answers.
Andrew Coleson
Bremen: I would consider that a surprising result (and not in a good way). No wonder I don't actually code in VB.
Greg Hewgill
+5  A: 

An index doesn't have any meaning to an IEnumerable, which is what the foreach construct uses. That's important because foreach may not enumerate in index order, if your particular collection type implements IEnumerable in an odd way. If you have an object that can be accessed by index and you care about the index during an iteration, then you're better off just using a traditional for loop:

for (int i=0;i<MyProperty.PropertyActors.Length;i++)
{
    //...
}
Joel Coehoorn
A: 

You could use the "FindIndex" method.

MyProperty.PropertyActors.FindIndex(Function(propActor As JCPropertyActor) propActor  = JCPropertyActor)

But inside of a for each loop that seems like alot of extra overhead, and seems like the same resulting problem as the "IndexOf" method. I suggest using old fashioned index iteration. This way you have your index and your item.

Dim PropertyActor As JCPropertyActor
For i As Integer = 0 To MyProperty.PropertyActors.Count - 1
    PropertyActor = MyProperty.PropertyActors.Item(i)
Next
John Chuckran
A: 

Index is only available on List objects, so PropertyActor is not a List object and for that does not have an index, what you can do however is get the index of the List object that contains the current propertyActor object like:

i = MyProperty.PropertyActors.IndexOf(PropertyActor)

balexandre