For Each ... Next 迴圈用於迴圈收集專案
你可以使用 For Each...Next
迴圈迭代任何 IEnumerable
型別。這包括陣列,列表以及 IEnumerable 型別或返回 IEnumerable 的任何其他內容。
迴圈遍歷 DataTable 的 Rows 屬性的示例如下所示:
For Each row As DataRow In DataTable1.Rows
'Each time this loops, row will be the next item out of Rows
'Here we print the first column's value from the row variable.
Debug.Print(Row.Item(0))
Next
需要注意的一點是,在 For Each
迴圈中不得修改集合。這樣做會導致 System.InvalidOperationException
顯示以下訊息:
收藏被修改; 列舉操作可能無法執行。