A For Each loop that iterates in reverse order

A For Each loop that iterates in reverse order

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:

Class ReverseIterator    Implements IEnumerable    ' a low-overhead ArrayList to store references    Dim items As New ArrayList()    Sub New(ByVal collection As IEnumerable)        ' load all the items in the ArrayList, but in reverse order        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 the enumerator of the inner ArrayList        Return items.GetEnumerator()    End FunctionEnd Class

Using the ReverseIterator class is as simple as passing the original collection object to its constructor:

' use an array in this simple testDim arr() As Integer = {1, 2, 3, 4, 5}Dim i As Integer' visit array elements in reverse orderFor Each i In New ReverseIterator(arr)    Console.WriteLine(i)Next

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as