I don\'t believe this is possible by conventional methods, but something like this verbose code:
For Each s As String In myStringList Step -1
//\' Do stu
You'd have to do something like:
For i as integer = myStringList.Count-1 to 0 step -1
dim s as string = myStringList.Item(i)
' Do your stuff with s
Next i
But as far as I know, you can't do a "For...Each" backwards, though that would be nice in a few instances.
' create a list of whatever and step through For Loop Collecting
dim revList as New List (of ToolStripItem)
For each objItem as ToolStripItem in Menu.DropDownItems
revList.Add(objItem)
next
revList.reverse ' reverse the order of that list
' step through the reverse ordered list
for each objItem as ToolStripItem in revList
' do whatever (like removing your menu from an ArrayMenu)
next
' replace ToolStripItem and Menu.DropDownItems with whatever you need
I think the documentation referenced in Mike's answer below is extremely misleading. The order of For Each
is defined by the collection it's called (i.e. its implementation of IEnumerable
/IEnumerable<T>
), but that's not the same as saying it shouldn't be used when the order is important. Many collections (such as arrays, List<T>
etc) always go in the "natural" order.
Part of the documentation does allude to this:
Traversal Order. When you execute a For Each...Next loop, traversal of the collection is under the control of the enumerator object returned by the GetEnumerator method. The order of traversal is not determined by Visual Basic, but rather by the MoveNext method of the enumerator object. This means that you might not be able to predict which element of the collection is the first to be returned in element, or which is the next to be returned after a given element.
That's not at all the same as saying it can't be relied upon - it can be relied upon if you know that the collection you're iterating over will produce the results in the desired order. It's not like it's going to pick elements at random. The behaviour in terms of IEnumerable
/IEnumerable<T>
is clearly defined on that page.
The most important exceptions to predictable orderings are dictionaries and sets, which are naturally unordered.
To reverse an IEnumerable<T>
, use Enumerable.Reverse - but if you need to iterate in reverse over a collection which is indexed by position (such as an array or List<T>
) then it would be more efficient to use a For
loop starting at the end and working backwards.
first you should create a list(of string) in for each statement, and after that make another normal for .. step -1 ..next statement. see an example:
Dim ff As New List(Of Integer)
For Each rRow As DataRow In DeletedRows
Dim item As Integer
item = rRow.Table.Rows.IndexOf(rRow)
ff.Add(item)
Next
For i As Integer = ff.Count - 1 To 0 Step -1
dim item as integer=ff(i)
next i
i hope that be helpful
The reason it can't be done is that, as a basic design feature, For Each can iterate over an enumerable without a last element, or with an as yet unknown last element.
Depending on what happens inside your loop you can do an .InsertAt(object,0) instead of an .Add and produce the same result as a reverse enumeration.