-1

forループ内にfor eachループがあり、for eachループは、現在の行のある列の値が配列に存在するかどうかをチェックし、存在する場合は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 を持つ行をスキップすることです。

4

5 に答える 5

1

一致する場合にのみ「forループを実行」しますか?なぜこのようにしないのですか?

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」に表示されるかどうかを判別する関数。リストですか?IDを使用できますか。Contains()
  • 「forループのことを行う」方法
于 2012-08-09T05:39:02.207 に答える
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
于 2012-08-09T05:27:36.907 に答える
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 
于 2012-08-09T05:27:52.407 に答える
0

終了ステートメント (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
于 2012-08-09T05:28:27.670 に答える
0

答えは問題なく読みやすいですが、doThings実際に使用するには、内側のループを VB.NET で使用できる他のループのいずれかContinue Forに変更する必要があります。For

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 で構文をチェックせずに入力します。)

于 2012-09-06T08:27:29.860 に答える