Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vb.net: index number in "for each"

Tags:

Sometime in VB.net i have something like:

For Each El in Collection    Write(El) Next 

But if i need the index number, i have to change it to

For I = 0 To Collection.Count() - 1    Write(I & " = " & Collection(I)) Next 

Or even (worse)

I = 0 For Each El In Collection    Write(I & " = " & El)    I += 1 Next 

Is there another way of getting the index?

like image 269
ariel Avatar asked Feb 25 '10 02:02

ariel


1 Answers

If you are using a generic collection (Collection(of T)) then you can use the IndexOf method.

For Each El in Collection    Write(Collection.IndexOf(El) & " = " & El) Next 
like image 108
Jim Counts Avatar answered Oct 19 '22 23:10

Jim Counts