vb.net继续为每个内部a

时间:2012-08-09 05:22:46

标签: vb.net for-loop continue

我在for循环中有一个for循环,for循环检查当前行中某些列中的值是否存在于数组中,如果它存在我想在for循环中执行什么操作,如果它不是我希望继续for循环

for i = 0 To DataGridView1.RowCount - 1
        For Each id In IDs
            If (DataGridView1.Item(1, i).Value <> id) Then
               'continue the for loop, by continue i mean using continue statement and not executing the outer for loop for this case
            End If
        Next
  'do for loop things
Next

我要做的是对具有特定id的行进行计算,并使用不在数组中的id跳过行。

5 个答案:

答案 0 :(得分:1)

您可以使用Exit For退出内部For Each循环。外部For循环将从中断处继续。

退出

  

可选。将控制转移出For Each循环。

     

在嵌套For Each循环中使用时,Exit For会导致执行退出最内层循环并将控制转移到下一个更高级别的嵌套。

http://msdn.microsoft.com/en-us/library/5ebk1751.aspx

for i = 0 To DataGridView1.RowCount - 1
        For Each id In IDs
            If (DataGridView1.Item(1, i).Value <> id) Then
               Exit For 'continue the for loop
            End If
        Next
  'do for loop things
Next

答案 1 :(得分:1)

只有在匹配的情况下,你想“为循环事做”吗?为什么不这样呢?

For i = 0 To DataGridView1.RowCount - 1 
    doThings = False
   For Each id In IDs
      If (DataGridView1.Item(1, i).Value = id) Then 
        doThings = True
        Exit For
      End If 
  Next 
  If doThings Then 
    ** do for loop things 
  End If 
Next 

可以通过创建更多方法来改进

  • 确定ID是否出现在“ID”中的函数。这是一个清单吗?你能使用IDs.Contains()
  • 吗?
  • “为循环做事”的方法

答案 2 :(得分:0)

不是检查不相等的条件,而是检查是否有相同的条件。

for i = 0 To DataGridView1.RowCount - 1         
    For Each id In IDs             
        If (DataGridView1.Item(1, i).Value == id) Then
            'continue the for loop
        End If
    Next
    'do for loop things 
Next 

答案 3 :(得分:0)

Exit Statement (Visual Basic) - MSDN

for i = 0 To DataGridView1.RowCount - 1
    For Each id In IDs
        If (DataGridView1.Item(1, i).Value <> id) Then
           Exit For ' Exit this For Each loop
        End If
    Next
'do for loop things
Next

答案 4 :(得分:0)

doThings答案很好且可读,但要实际使用Continue For,您需要将内部For循环更改为可用于VB.NET的其他循环之一。< / p>

for i = 0 To DataGridView1.RowCount - 1
    Using EnumId As IEnumerator(Of String) = IDs.GetEnumerator
        'For Each id In IDs
      While EnumId.MoveNext
        Dim id = EnumId.Current
            If (DataGridView1.Item(1, i).Value <> id) Then
               'continue the for loop, by continue i mean using continue statement and not executing the outer for loop for this case
               Continue For
            End If
        'Next
      End While
    End Using
  'do for loop things
Next

(在IDE中输入而不检查语法。)