I believe the newer For each has an Index output property.
but, if not, then you can store this at the beginning of the for loop using .IndexOf()
For example,
For each item in arr
index = Array.IndexOf(arr, item)
However, this needs to be adjusted for String and Int loops because it will find the first item in the array if there are duplicates in the array. You can adjust this by using the startIndex property.
Array.IndexOf(arr, item, index+1)
Then, since it will be starting the loop again, you can use .Skip() to jump to the index that was previously stored.
For example (assuming index starts on -1),
For each item in arr.Skip(If(index<0, 0, index))
index = Array.IndexOf(arr, item, index+1)
If condition
Break
A datatable loop will look like this:
For each row in dt1.AsEnumerable.Skip(If(rowIndex<0, 0, rowIndex))
rowIndex = dt1.Rows.IndexOf(row)
If condition
Break
This is also useful for error handling when an error might push your process out of the sequence and need to start over again.
You can also use a counter instead of using the .IndexOf(), but I don’t know if that is a good approach to be honest for ForEach loops.
So, something like that.
Regards.