programing

VB.NET-For Each 루프의 다음 항목으로 이동하는 방법?

nasanasas 2020. 9. 11. 08:07
반응형

VB.NET-For Each 루프의 다음 항목으로 이동하는 방법?


Exit For루프를 종료하는 대신 다음 항목으로 이동하는 것을 제외하고 와 같은 문구가 있습니까?

예를 들면 :

For Each I As Item In Items

    If I = x Then 
        ' Move to next item
    End If

    ' Do something

Next

Else다음과 같이 읽을 수 있도록 If 문에 간단히 추가 할 수 있습니다.

For Each I As Item In Items

    If I = x Then 
        ' Move to next item
    Else
        ' Do something
    End If

Next

Items목록 의 다음 항목으로 이동하는 방법이 있는지 궁금 합니다. 나는 대부분의 사람들이 왜 그 Else문장을 사용하지 않는지에 대해 제대로 질문 할 것이라고 확신 하지만, "Do Something"코드를 래핑하는 것은 읽기 어려운 것처럼 보인다. 특히 더 많은 코드가있을 때.


For Each I As Item In Items
    If I = x Then Continue For

    ' Do something
Next

내가 사용하는 것 Continue대신에 문을 :

For Each I As Item In Items

    If I = x Then
        Continue For
    End If

    ' Do something

Next

Note that this is slightly different to moving the iterator itself on - anything before the If will be executed again. Usually this is what you want, but if not you'll have to use GetEnumerator() and then MoveNext()/Current explicitly rather than using a For Each loop.


What about:

If Not I = x Then

  ' Do something '

End If

' Move to next item '

I want to be clear that the following code is not good practice. You can use GOTO Label:

For Each I As Item In Items

    If I = x Then
       'Move to next item
        GOTO Label1
    End If

    ' Do something
    Label1:
Next

When I tried Continue For it Failed, I got a compiler error. While doing this, I discovered 'Resume':

For Each I As Item In Items

    If I = x Then
       'Move to next item
       Resume Next
    End If

    'Do something

Next

Note: I am using VBA here.

참고URL : https://stackoverflow.com/questions/829689/vb-net-how-to-move-to-next-item-a-for-each-loop

반응형