none
Redo same iteration of For RRS feed

  • Question

  • In a For Each, Continue For moves to the next iteration of the loop. Is there a way to redo the same iteration?
    • Edited by Brian Tkatch Thursday, September 5, 2019 9:18 PM typo and didn;t mention it was a for each
    Thursday, September 5, 2019 9:00 PM

All replies

  • Hello,

    To best answer your question please provide more details.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 5, 2019 9:03 PM
    Moderator
  • For Each A in B
     step 1
     step 2
     If not check_something() the redo steps 1 and 2
    Next


    Thursday, September 5, 2019 9:08 PM
  • Continue For to the next iteration of the loop. Is there a way to redo the same iteration?

    Hi

    Not very useful, but here is one way. In this example, if the iterator = 3 then subtract 1 from it. Puts the itersators in a ListBox to test.

    Dim pass As Boolean = True
    For i As Integer = 1 To 6
    	If i = 3 AndAlso pass Then
    		pass = False
    		i -= 1
    	End If
    ListBox1.Items.Add(i)
    Next
    


    Regards Les, Livingston, Scotland

    Thursday, September 5, 2019 9:11 PM
  • Sorry, seems i made a typo.

    The case i am dealing with in a For Each.

    Thursday, September 5, 2019 9:17 PM
  • Sorry, seems i made a typo.

    The case i am dealing with in a For Each.

    Hi

    Even less elegant!

    Dim lst As New List(Of Integer)({1, 2, 3, 4, 5, 6})
    Dim pass As Boolean = True
    For Each a As Integer In lst
    jump: ListBox1.Items.Add(a)
    	If pass AndAlso a = 3 Then
    		pass = False
    		GoTo jump ' repeat with a=3
    	End If
    Next
    


    Regards Les, Livingston, Scotland

    Thursday, September 5, 2019 9:26 PM
  • Yeah, i figure goto with a label works. Probably a decent case to use it too.

    After i posted it, i figured maybe i could just put a second loop:

    Dim lst As New List(Of Integer)({1, 2, 3, 4, 5, 6})
    Dim pass As Boolean = False
    For Each a As Integer In lst
     Do Until pass
    	If pass AndAlso a = 3 Then pass = True
     Loop
    Next


    • Edited by Brian Tkatch Thursday, September 5, 2019 9:33 PM
    Thursday, September 5, 2019 9:33 PM
  • Hi,

    I'm afraid your code leads to endless loop.  How about using counter instead of "Do-Until"?

    Module Module1
        Sub Main()
            Dim lst As New List(Of Integer)({1, 2, 3, 4, 5, 6})
            Dim cnt As Integer = 0
            For Each a As Integer In lst
                cnt += 1
                Console.WriteLine("a=" & a & "cnt=" & cnt)
                If (a = 3) Then
                    Console.WriteLine("a=" & a & "cnt=" & cnt)
                End If
            Next
            Console.Read()
        End Sub
    End Module
        


    Regards,


    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, September 5, 2019 10:20 PM
  • Sorry, hacked it in quickly to show the concept. :)

    I ended up using another For, specifically:

    For Attempt As Integer = 1 To 3
     ...
     ...
     If Check(argument) Then Exit For
    Next

    This way, it never tried more than 3 times, and exits on success. 

    Thursday, September 5, 2019 10:29 PM
  • For Each A in B
     step 1
     step 2
     If not check_something() the redo steps 1 and 2
    Next


    One thing that people (note I didn't say developers) without much expertise with coding think that in cases like this they are constrained to a for-each or for-next where it would be better to split things into separate logical parts. For instance, in this case we have a list were if the person is active do specific steps while if not active do something else.

    With that in mind the code below to show the concept (and I'm not saying this will fit into what you want but instead a concept) of moving away from one loop/next to separate parts.

    This code requires Framework 3.5 or higher, if you don't have 3.5 then perhaps it's time to consider updating to Visual Studio 2019 Community edition which is free and will function with older projects.

    Class to work with

    Public Class Person
        Public Property Name() As String
        Public Property Active() As Boolean
    
        Public Overrides Function ToString() As String
            Return Name
        End Function
    End Class
    

    In this case mocked data to demonstrate the concept. Running with a form, one button.

    Public Class Form1
        Private ReadOnly _people As New List(Of Person) From
            {
                New Person() With {.Name = "Jim", .Active = True},
                New Person() With {.Name = "Anne", .Active = False},
                New Person() With {.Name = "Frank", .Active = True}
            }
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) _
            Handles Button1.Click
    
            _people.Where(Function(person) person.Active).
                ToList().
                ForEach(Sub(person)
                            Console.WriteLine($"{person} is active do something")
                        End Sub)
            _people.Where(Function(person) person.Active = False).
                ToList().
                ForEach(Sub(person)
                            Console.WriteLine($"{person} is NOT active do something")
                        End Sub)
        End Sub
    End Class

    Results

    Jim is active do something
    Frank is active do something
    Anne is NOT active do something
    

    So we then can do say

    Public Class Form1
        Private ReadOnly _people As New List(Of Person) From
            {
                New Person() With {.Name = "Jim", .Active = True},
                New Person() With {.Name = "Anne", .Active = False},
                New Person() With {.Name = "Frank", .Active = True}
            }
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) _
            Handles Button1.Click
    
            _people.Where(Function(person) person.Active).
                ToList().
                ForEach(Sub(person)
                            Step1(person)
                            Step2(person)
                        End Sub)
            _people.Where(Function(person) person.Active = False).
                ToList().
                ForEach(Sub(person)
                            Step2(person)
                        End Sub)
        End Sub
        Private Sub Step1(person As Person)
            ' TODO
        End Sub
        Private Sub Step2(person As Person)
            ' TODO
        End Sub
    End Class


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 5, 2019 10:49 PM
    Moderator
  • Not sure what you are doing here. My mind isn't completely into it though, as i am still working on the program. :)

    In my case, i get logins from a database, login to a website, generate a list of objects, then proceed to run a report for each of those objects. So its a for each login, for each object, run report. The report can fail for a variety of reasons, which in some cases i'd just like to try again.

    In a separate reply below i figured out the answer that i needed. I added a for x = 1 to 3 loop to try three times, and exit on success. It's working nicely, though i may change it to five tries. Websites can be moody. :)

    Thursday, September 5, 2019 11:00 PM
  • Hi,

    I'm afraid it is not "For-Each".
    I'd like to confirm what your true question is, and what you want to do by "Redo" in "For" loop.
    I hope you will show some cases where "Redo" is required.

    Regards,

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, September 5, 2019 11:06 PM
  • I have a for each which iterates through a collection. It then attempts to do something with the individual items. When it fails, i want to try again.

    I ended up adding a For...Next inside the For Each to control the attempts. So, it is something like:

    For Each Item in Collection
      For Attempt = 1 to 3
        If Do_something(Item) Then Exit For
      Next
    Next

    Thursday, September 5, 2019 11:12 PM
  • Hi,

    If you want to put restriction on sign-in with password, i.e. if users input incorrect password over 3 times, they cannot sign-in, you don't have to use "For Each", only need "For Attempt = 1 to 3".

    Thinking simple would be better.

    Regards, 

    Ashidacchi -- https://ssl01.rocketnet.jp/hokusosha.com/default.html

    Thursday, September 5, 2019 11:35 PM
  • This is an automated program that logs on as other users to download reports for them. The retry is if the download fails. (The download must be setup and generated first, and there are a few steps to the process.)

    The for each is for each user, the for...next is for each attempt.

    Thursday, September 5, 2019 11:45 PM
  • Not sure what you are doing here. My mind isn't completely into it though, as i am still working on the program. :)


    I have broken out conditions into two .List(Of Person).ForEach method.

    Here is the docs, if the page comes up in C# change it at the top right of the page to VB.

    https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.foreach?view=netframework-4.8

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 5, 2019 11:47 PM
    Moderator

  • In my case, i get logins from a database, login to a website, generate a list of objects, then proceed to run a report for each of those objects. So its a for each login, for each object, run report. The report can fail for a variety of reasons, which in some cases i'd just like to try again.

    In a separate reply below i figured out the answer that i needed. I added a for x = 1 to 3 loop to try three times, and exit on success. It's working nicely, though i may change it to five tries. Websites can be moody. :)

    Well this would have been good enough in your initial question!!!

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, September 5, 2019 11:47 PM
    Moderator