|
|
|
The For Each loop always iterates orderly on all the elements of an array or a collection (more precisely, on all the elements of a class that implements the IEnumerable interface). What happens if you need to iterate in reverse order, though? If you have an array or an ArrayList you can always reference items by their numerical index, but not all the collection objects give you this ability (for example, you can't access Dictionary items by an index). The following class works as a wrapper for any IEnumerable class and lets you visit all its items in reverse order: |
Click here to copy the following block | Class ReverseIterator Implements IEnumerable
Dim items As New ArrayList()
Sub New(ByVal collection As IEnumerable) Dim o As Object For Each o In collection items.Insert(0, o) Next End Sub
Public Function GetEnumerator() As System.Collections.IEnumerator _ Implements System.Collections.IEnumerable.GetEnumerator Return items.GetEnumerator() End Function End Class |
Using the ReverseIterator class is as simple as passing the original collection object to its constructor: |
|
|
|
Submitted By :
Nayan Patel
(Member Since : 5/26/2004 12:23:06 PM)
|
|
|
Job Description :
He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting. |
View all (893) submissions by this author
(Birth Date : 7/14/1981 ) |
|
|