VB.NET - How to move to next item a For Each Loop?

后端 未结 6 2000
后悔当初
后悔当初 2020-12-13 22:50

Is there a statment like Exit For, except instead of exiting the loop it just moves to the next item.

For example:

For          


        
6条回答
  •  感动是毒
    2020-12-13 23:43

    Only the "Continue For" is an acceptable standard (the rest leads to "spaghetti code").

    At least with "continue for" the programmer knows the code goes directly to the top of the loop.

    For purists though, something like this is best since it is pure "non-spaghetti" code.

    Dim bKeepGoing as Boolean 
    For Each I As Item In Items
      bKeepGoing = True
      If I = x Then
        bKeepGoing = False
      End If
      if bKeepGoing then
        ' Do something
      endif
    Next
    

    For ease of coding though, "Continue For" is OK. (Good idea to comment it though).

    Using "Continue For"

    For Each I As Item In Items
      If I = x Then
        Continue For   'skip back directly to top of loop
      End If
      ' Do something
    Next
    

提交回复
热议问题