这应该不会突破内循环吗?

时间:2012-03-06 21:41:48

标签: vb.net

我需要突破内循环,并且我被告知退出循环不是最好的方法。所以我想到了另一种方法,我想在if语句中获取值,但它似乎没有正常工作。

 While Bavailable = True

            For Each position In MyPosList

                If creditPoints = "20" And semester = "1" And year = "Year 1" Then
                    Blongthin = False
                    BlongFat = False
                    If position.strLabel = "a1" And available(0) = "True" Then

                        pos = position.strX & " " & position.strY
                        count += 1
                        available(0) = blnavailable
                        Bavailable = False
                        MsgBox(Bavailable)

                    ElseIf position.strLabel = "b1" And available(1) <> "False" Then
                        pos = position.strX & " " & position.strY
                        'position.blnAvail = False
                        count += 1
                        available(1) = blnavailable
                        Bavailable = False

                    ElseIf position.strLabel = "c1" And available(2) <> "False" Then
                        pos = position.strX & " " & position.strY
                        position.blnAvail = False
                        count += 1
                        available(2) = blnavailable
                        Bavailable = False
                    End If

你看到我正在使用Bavailable尝试突破循环以获得其中一个的实例中的值pos如果条件返回true?有什么帮助吗?

4 个答案:

答案 0 :(得分:1)

我不同意这个前提。如果你使用那个外部while循环的完整原因是为了避免使用Exit For那么你应该摆脱While。如果您在For中的条件确实非常长,并且您希望避免跳出比运动更多的话,那就这样做:

For Each position In MyPosList 

   Dim breakEarly As Boolean = False

   If someCondition Then

      'logic, logic, logic
      'logic, logic, logic
      'logic, logic, logic
      breakEarly = True

   ElseIf someOtherCondition Then

      'logic, logic, logic
      'logic, logic, logic
      'logic, logic, logic
      breakEarly = True

   ElseIf etc, 
   ElseIf etc, 
   ElseIf etc, 
   End If

   If breakEarly Then

      'do any common pre-exit code here

      'now bail
      Exit For

   End If

Next

答案 1 :(得分:0)

当你不想循环所有项目时,我不喜欢使用For循环。在这种情况下,我更喜欢使用While。怎么样:

Dim index As Integer
index = 0

While Not Found And index < MyPosList.Count
    ' Do whatever and update Found when required
    index = index + 1
While End

答案 2 :(得分:0)

尝试:

    While Bavailable
        For Each position In MyPosList
            If Bavailable Then  'Add loop controls to inner loop
                If creditPoints = "20" And semester = "1" And Year() = "Year 1" Then
                    Blongthin = False
                    BlongFat = False
                    If position.strLabel = "a1" And available(0) = "True" Then
                        pos = position.strX & " " & position.strY
                        count += 1
                        available(0) = blnavailable
                        Bavailable = False
                        MsgBox(Bavailable)

                    ElseIf position.strLabel = "b1" And available(1) <> "False" Then
                        pos = position.strX & " " & position.strY
                        'position.blnAvail = False
                        count += 1
                        available(1) = blnavailable
                        Bavailable = False

                    ElseIf position.strLabel = "c1" And available(2) <> "False" Then
                        pos = position.strX & " " & position.strY
                        position.blnAvail = False
                        count += 1
                        available(2) = blnavailable
                        Bavailable = False
                    End If
                End If
            End If
        Next position
    End While

虽然我对Exit ForExit While ......

的确切错误非常感兴趣

答案 3 :(得分:0)

如果Exit ForExit While使您的代码更具可读性,请使用它们! 没有意义添加一个唯一目的会增加不必要的复杂性的变量。

如果您可以将大部分内部逻辑移动到单独的功能中,那就更好了。

相关问题