vb net从内循环中断,然后移动到外循环

时间:2014-01-05 06:11:40

标签: vb.net

大家好我从这个网站的另一个帖子中得到了一个类似的问题,所以我从那里做了一个例子,并在...中添加了我的问题。

这里是

For Each t as trans in transaction
  For each h as item2 in items2
    For Each I As Item In Items
       If I = h Then 
       'i wanna break the for then move to next item2(h) and increment the i
       else
       'i wanna break the for then move to next trans(t) and reset the i starts from 1
       end if

    Next
  Next
next

我已经尝试过了,但它不起作用(GoTo,boolean)

For Each t as trans in transaction
    For each h as item2 in items2
      For Each I As Item In Items
         If I = h Then 
         i+=1
         GoTo flag1
         else
         i=1
         GoTo flag2
         end if
      Next
flag1:
    Next
flag2:
next
请帮助我解决这个问题,谢谢之前 致电@Neolisk @ har07

2 个答案:

答案 0 :(得分:0)

Start:    For Each t as trans in transaction
            For each h as item2 in items2
             For Each I As Item In Items
               If I = h Then 
                 i+=1;
                 exit for
               else
                  Goto Start
               end if
            Next
          Next
        next

Exit For将打破内部for循环,而在其他部分你可以使用Label方法从头开始执行...但这可能会导致overflow exception < / p>

答案 1 :(得分:0)

使用布尔标志来控制它:

Dim flag As Boolean = False
For Each t As Trans In Transaction
    For Each h As Item2 In Items2
        For Each i As Item In Items
            flag = false
            If i = h Then 
                Exit For
            Else
                flag = True
                Exit For
            End If
        Next
        If flag Then Exit For
    Next
Next