在 vb.net 中中断/退出嵌套
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5312291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Breaking/exit nested for in vb.net
提问by KoolKabin
How do I get out of nested for or loop in vb.net?
如何摆脱 vb.net 中的嵌套 for 或循环?
I tried using exit for but it jumped or breaked only one for loop only.
我尝试使用 exit for 但它只跳或中断了一个 for 循环。
How can I make it for the following:
我怎样才能做到以下几点:
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
回答by Heinzi
Unfortunately, there's no exit two levels of for
statement, but there are a few workarounds to do what you want:
不幸的是,没有exit two levels of for
声明,但有一些解决方法可以做你想做的事:
Goto. In general, using
goto
is considered to be bad practice(and rightfully so), but usinggoto
solely for a forward jump out of structured control statements is usually considered to be OK, especially if the alternative is to have more complicated code.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:
Dummy outer block
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
or
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
Separate function: Put the loops inside a separate function, which can be exited with
return
. This might require you to pass a lot of parameters, though, depending on how many local variables you use inside the loop. An alternative would be to put the block into a multi-line lambda, since this will create a closure over the local variables.Boolean variable: This might make your code a bit less readable, depending on how many layers of nested loops you have:
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
转到。一般来说, using
goto
被认为是不好的做法(并且理所当然),但goto
仅用于结构化控制语句的向前跳转通常被认为是可以的,特别是如果替代方案是具有更复杂的代码。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
。不过,这可能需要您传递大量参数,具体取决于您在循环内使用的局部变量的数量。另一种方法是将块放入多行 lambda 中,因为这将在局部变量上创建一个闭包。布尔变量:这可能会使您的代码可读性稍差,具体取决于您有多少层嵌套循环:
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
回答by Tobias Schittkowski
Put the loops in a subroutine and call return
将循环放在子程序中并调用 return
回答by Andrew Thomas
Make the outer loop a while loop, and "Exit While" in the if statement.
使外部循环成为 while 循环,并在 if 语句中“退出 While”。
回答by Cesar
I've experimented with typing "exit for" a few times and noticed it worked and VB didn't yell at me. It's an option I guess but it just looked bad.
我已经尝试过几次输入“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.
我认为最好的选择类似于 Tobias 所分享的。只需将您的代码放在一个函数中,并在您想跳出循环时让它返回。看起来也更干净。
For Each item In itemlist
For Each item1 In itemlist1
If item1 = item Then
Return item1
End If
Next
Next
回答by Hitesh Paliwal
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
回答by Poppa Mintin
If I want to exit a for-to loop, I just set the index beyond the limit:
如果我想退出 for-to 循环,我只需将索引设置为超出限制:
For i = 1 To max
some code
if this(i) = 25 Then i = max + 1
some more code...
Next`
Poppa.
爸爸。