Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3303790
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:59:38+00:00 2026-05-17T20:59:38+00:00

How can I determine if I’m in the final loop of a For Each

  • 0

How can I determine if I’m in the final loop of a For Each statement in VB.NET?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T20:59:39+00:00Added an answer on May 17, 2026 at 8:59 pm

    The generally, collections on which you can perform For Each on implement the IEnumerator interface. This interface has only two methods, MoveNext and Reset and one property, Current.

    Basically, when you use a For Each on a collection, it calls the MoveNext function and reads the value returned. If the value returned is True, it means there is a valid element in the collection and element is returned via the Current property. If there are no more elements in the collection, the MoveNext function returns False and the iteration is exited.

    From the above explanation, it is clear that the For Each does not track the current position in the collection and so the answer to your question is a short No.

    If, however, you still desire to know if you’re on the last element in your collection, you can try the following code. It checks (using LINQ) if the current item is the last item.

    For Each item in Collection
        If item Is Collection.Last Then
            'do something with your last item'
        End If
    Next
    

    It is important to know that calling Last() on a collection will enumerate the entire collection. It is therefore not recommended to call Last() on the following types of collections:

    • Streaming collections
    • Computationally expensive collections
    • Collections with high tendency for mutation

    For such collections, it is better to get an enumerator for the collection (via the GetEnumerator() function) so you can keep track of the items yourself. Below is a sample implementation via an extension method that yields the index of the item, as well as whether the current item is the first or last item in the collection.

    <Extension()>
    Public Iterator Function EnumerateEx(Of T)(collection As IEnumerable(Of T))
        As IEnumerable(Of (value As T, index As Integer, isFirst As Boolean, isLast As Boolean))
    
        Using e = collection.GetEnumerator()
            Dim index = -1
            Dim toYield As T
    
            If e.MoveNext() Then
                index += 1
                toYield = e.Current
            End If
    
            While e.MoveNext()
                Yield (toYield, index, index = 0, False)
                index += 1
                toYield = e.Current
            End While
    
            Yield (toYield, index, index = 0, True)
        End Using
    End Function
    

    Here is a sample usage:

    Sub Main()
        Console.WriteLine("Index   Value   IsFirst   IsLast")
        Console.WriteLine("-----   -----   -------   ------")
    
        Dim fibonacci = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89}
    
        For Each i In fibonacci.EnumerateEx()
            Console.WriteLine(String.Join("   ", $"{i.index,5}",
                                                 $"{i.value,5}",
                                                 $"{i.isFirst,-7}",
                                                 $"{i.isLast,-6}"))
        Next
    
        Console.ReadLine()
    End Sub
    

    Output

    Index   Value   IsFirst   IsLast
    -----   -----   -------   ------
        0       0   True      False
        1       1   False     False
        2       1   False     False
        3       2   False     False
        4       3   False     False
        5       5   False     False
        6       8   False     False
        7      13   False     False
        8      21   False     False
        9      34   False     False
       10      55   False     False
       11      89   False     True
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In ASP.NET MVC, is it possible to define routes that can determine which controller
In .NET, I think I can determine if a file is a symbolic link
I can determine the current number of connections by db.serverStatus().connections but all that gives
Can somebody please let me know how we can determine if a local disk
How can I determine the address in memory of the Java heap for a
How can I determine programmatically whether my machine is an x86, x64 or an
How can you determine the minimum version of Java required on a client's browser
How can you determine which rubygem is being used in response to a require
How can I determine type of mysql database : whether it is InnoDB or
How can I determine/calculate the byte size of a bitmap (after decoding with BitmapFactory)?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.