我們大多數開發(fā)人員在進行VB.NET進行開發(fā)的時候,當遇到循環(huán)操作時,可能會第一時間想到VB.NET For/Next的應用。不過,在某些特殊情況下,可以使用VB.NET For/Each來實現(xiàn)對一個數組或集合中元素的遍歷。
VB.NET For/Each語句的寫法如下:
例2.10(02-10.aspx)用VB.NET For/Each顯示一個數組中的所有數據。
- < %
- Dim arrData(3)
- Dim stritem as string
- arrData(0)="Beijing"
- arrData(1)="Shanghai"
- arrData(2)="Guangzhou"
- For Each stritem In arrData
- Response.Write (stritem &
"< br>")- Next
- %>
可以看出,VB.NET For/Each環(huán)與For/Next循環(huán)的區(qū)別是:在For/Next循環(huán)中需要指明循環(huán)的次數,而在For/Each循環(huán)中不需要這樣就可以遍歷到一個數組或集合的所有內容。另外需要說明的是,這種循環(huán)通常在集合中使用。