Him_Jalpert
Him_Jalpert

Reputation: 2516

Is there a way to start over in a For Each?

I have a For Each statement that I want to be able to start from the beginning of when a certain option is selected in my combobox. Is this possible?

For Each cbo As ComboBox In s_combos
        For Each cov As String In s_coverage

            If combo.Name = cbo.Name And combo.SelectedIndex = 0 Then
                If txtNewMoney.Text <> "" And b_isSmoker = False Then
                    CalculateRunningTotal(False, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
                ElseIf txtNewMoney.Text <> "" And b_isSmoker = True Then
                    CalculateRunningTotal(True, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
                End If
                counter += 1
                Exit For
            End If
        Next


        If combo.Name = cbo.Name And combo.SelectedIndex = 1 Then
           'If this option is selected, start over in For Each
            counter -= 1
            Dim dec As frmDeclined = New frmDeclined(DirectCast(sender, Control).Name, Me)
            dec.ShowDialog()
        End If
Next

Upvotes: 0

Views: 358

Answers (3)

xpda
xpda

Reputation: 15813

It's usually considered bad manners to modify the index of a For loop from inside the loop.

One socially acceptable way to do this is to use a While loop with an independent variable incremented or reset within the loop.

If you definitely want to use a For...Each loop, you could put a While loop outside it to be iterated to effectively reset the For...Each loop. This would be unnecessarily complicated in almost every case I can think of, and I generally would not recommend it.

Upvotes: 1

Steve
Steve

Reputation: 5545

You could create a label and GOTO it:

GoHere:
For Each cbo As ComboBox In s_combos
        For Each cov As String In s_coverage

            If combo.Name = cbo.Name And combo.SelectedIndex = 0 Then
                If txtNewMoney.Text <> "" And b_isSmoker = False Then
                    CalculateRunningTotal(False, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
                ElseIf txtNewMoney.Text <> "" And b_isSmoker = True Then
                    CalculateRunningTotal(True, cov, , CKeyValuePair.GetComboBoxSelectedKey(s_applicants(counter), True))
                End If
                counter += 1
                Exit For
            End If
        Next


        If combo.Name = cbo.Name And combo.SelectedIndex = 1 Then
           'If this option is selected, start over in For Each
            counter -= 1
            Dim dec As frmDeclined = New frmDeclined(DirectCast(sender, Control).Name, Me)
            dec.ShowDialog()
            Goto GoHere
        End If
Next

Upvotes: -1

Karl Jacques
Karl Jacques

Reputation: 63

You could use a plain for loop instead, and set the iterator back to it's initial value. It's probably best that you restructure this to be more efficient though, is there any point looping the first time if the option is selected?

Upvotes: 2

Related Questions