VB.NET For/Each技術(shù)應(yīng)用詳解
我們大多數(shù)開(kāi)發(fā)人員在進(jìn)行VB.NET進(jìn)行開(kāi)發(fā)的時(shí)候,當(dāng)遇到循環(huán)操作時(shí),可能會(huì)***時(shí)間想到VB.NET For/Next的應(yīng)用。不過(guò),在某些特殊情況下,可以使用VB.NET For/Each來(lái)實(shí)現(xiàn)對(duì)一個(gè)數(shù)組或集合中元素的遍歷。#t#
VB.NET For/Each語(yǔ)句的寫(xiě)法如下:
- For Each item In
Array or Collection - [statements]
- Next
例2.10(02-10.aspx)用VB.NET For/Each顯示一個(gè)數(shù)組中的所有數(shù)據(jù)。
- < %
- 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)的次數(shù),而在For/Each循環(huán)中不需要這樣就可以遍歷到一個(gè)數(shù)組或集合的所有內(nèi)容。另外需要說(shuō)明的是,這種循環(huán)通常在集合中使用。