Can we keep track of our iteration in our loop when we use a For Each? I like to use the For Each loops for looping through my objects but I cannot seem to find a way to keep an index of where I'm at in the loop. Unless of course I create my own ...
If you want to have an index, use a For-loop instead of a For each. That's why it exists.
For i As Int32 = 0 To objects.Count - 1
Dim obj = objects(i)
Next
Of course nothing prevents you from creating your own counter:
Dim counter As Int32 = 0
For Each obj In objects
counter += 1
Next
or you can use Linq:
Dim query = objects.Select(Function(obj, index) "Index is:" & index)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With