2010-07-07 6 views
1

ASP.NET/VB 대하여 반복 컬렉션을 통해 결과를 Do..This Do..That 결과가 발견되지 않음 발견

[이것은 단순한 일례이다] 난에서 컬렉션 ("myCollection")를 가질 세 가지 항목이 있습니다 ("hello", "goodbye", "welcome"). 컬렉션을 반복하고 컬렉션에 "환영"항목이있는 경우 항목을 하나만 갖고 싶다면 다른 것을하고 싶습니다. 이처럼 (의사) :

For Each entry in myCollection 
    If entry="welcome" Then 
    DoSomething() 
    End If 
Next (If Not MsgBox("Bad!")) 

제안 사항?

답변

3

이 시도 :

Dim found as Boolean = false 
For Each entry in myCollection 
    If entry="welcome" Then 
    DoSomething() 
    found = True 
    Exit For ' Assumes only want to DoSomething for one "welcome" ' 
    End If 
Next 

If Not found Then 
    MsgBox("Bad!") 
End If `enter code here` 

을 대안 LINQ 버전이 더 간결 보일 수 있습니다 :

If myCollection.Contains("welcome") Then 
    DoSomething() 
Else 
    MsgBox("Bad!") 
End If