vb.net: index number in "for each"

collections, vb.net

Solution

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

Problem

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?

Original source