vb.net에서 중첩 / 종료
vb.net에서 중첩 된 for 또는 루프에서 벗어나려면 어떻게해야합니까?
나는 exit를 사용하려고 시도했지만 하나의 루프 만 점프하거나 끊었습니다.
다음을 위해 어떻게 할 수 있습니까?
for each item in itemList
for each item1 in itemList1
if item1.text = "bla bla bla" then
exit for
end if
end for
end for
불행히도, exit two levels of for
진술은 없지만 원하는 것을 수행 할 수있는 몇 가지 해결 방법이 있습니다.
고토 . 일반적으로 사용
goto
되는 나쁜 관행으로 간주 (당연히 그렇게하고)하지만 사용하여goto
일반적으로 다른 더 복잡한 코드를 가지고있다, 특히, OK로 간주되어 앞으로 구조 제어 문 밖으로 뛰어으로 만.For Each item In itemList For Each item1 In itemList1 If item1.Text = "bla bla bla" Then Goto end_of_for End If Next Next end_of_for:
더미 외부 블록
Do For Each item In itemList For Each item1 In itemList1 If item1.Text = "bla bla bla" Then Exit Do End If Next Next Loop While False
또는
Try For Each item In itemlist For Each item1 In itemlist1 If item1 = "bla bla bla" Then Exit Try End If Next Next Finally End Try
개별 기능 : 루프를 별도의 기능 안에 넣으십시오
return
. 그러나 루프 내에서 사용하는 로컬 변수의 수에 따라 많은 매개 변수를 전달해야 할 수도 있습니다. 대안은 블록을 여러 줄 람다에 넣는 것입니다. 로컬 변수에 대한 클로저가 만들어지기 때문입니다.부울 변수 : 중첩 루프의 레이어 수에 따라 코드를 읽기 어렵게 만들 수 있습니다.
Dim done = False For Each item In itemList For Each item1 In itemList1 If item1.Text = "bla bla bla" Then done = True Exit For End If Next If done Then Exit For Next
서브 루틴에 루프를 넣고 호출 return
외부 루프를 while 루프로 만들고 if 문에서 "Exit While"을 만듭니다.
"exit for"를 몇 번 입력하여 실험 한 결과 제대로 작동하고 VB가 소리 지르지 않는 것을 알았습니다. 그것은 내가 추측하는 옵션이지만 방금 나빠 보였습니다.
I think the best option is similar to that shared by Tobias. Just put your code in a function and have it return when you want to break out of your loops. Looks cleaner too.
For Each item In itemlist
For Each item1 In itemlist1
If item1 = item Then
Return item1
End If
Next
Next
For i As Integer = 0 To 100
bool = False
For j As Integer = 0 To 100
If check condition Then
'if condition match
bool = True
Exit For 'Continue For
End If
Next
If bool = True Then Continue For
Next
If I want to exit a for-to loop, I just set the index beyond the limit:
For i = 1 To max
some code
if this(i) = 25 Then i = max + 1
some more code...
Next`
Poppa.
참고URL : https://stackoverflow.com/questions/5312291/breaking-exit-nested-for-in-vb-net
'Programming' 카테고리의 다른 글
람다 식에는 코드 줄 저장 이외의 용도가 있습니까? (0) | 2020.07.18 |
---|---|
플레이시 휴식! (0) | 2020.07.18 |
ApiController로 원시 문자열을 반환하는 방법은 무엇입니까? (0) | 2020.07.18 |
로컬 지점을 GitHub로 푸시 (0) | 2020.07.18 |
팬더는 평균 / 평균을 얻는다 (0) | 2020.07.18 |