vb.net equivalent to c# continue

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lathamik
    New Member
    • May 2007
    • 1

    vb.net equivalent to c# continue

    Does anyone know of the vb.net equivelent to the c# continue so I can exit the immediate iteration of a for loop and continue with the next one.
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    Originally posted by lathamik
    Does anyone know of the vb.net equivelent to the c# continue so I can exit the immediate iteration of a for loop and continue with the next one.
    Exit For
    Code:
    		Dim j As Integer
    		For i As Integer = 1 To 4
    			If i = 3 Then Exit For
    			j = i
    		Next i
    		MsgBox(j) ' will give 2

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Sorry Sammy, close but no cigar. Continue doesn't drop out of the loop, it skips to the next iteration.

      In VB6, at least, I don't think there is an equivalent of continue. I seem to recall hearing recently about something in VB.Net which would do it, but don't remember what it was.

      In VB6, I tend to use an inner loop to achieve the same end. For example...
      Code:
      For Index = 1 to 10
        [B]Do[/B]
        If some condition Then
          [B]Exit Do[/B] ' Equivalent of applying Continue to the For loop.
        End If
        ... other processing that you wanted to skip ...
      
        [B]Loop While False[/B] ' In other words, [I]don't[/I] loop.
      Next
      It's a little ugly, but it works.

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        > Sorry Sammy, close but no cigar. Continue doesn't drop out of the loop, it skips to the next iteration.

        Yep, sorry, I didn't read past "exit"

        http://msdn2.microsoft.com/en-us/lib...6t(VS.71).aspx says
        "Visual Basic .NET does not support the Continue statement of previous versions of Visual Basic. However, you can achieve the same functionality by putting a statement label on the Loop statement and branching to it from the middle of the loop."

        Too bad it's in C#! Makes for ghasty code. And Microsoft's solution of a statement label is just as bad. Just rewrite the loop and make it readable.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by SammyB
          Too bad it's in C#! Makes for ghasty code. And Microsoft's solution of a statement label is just as bad. Just rewrite the loop and make it readable.
          I partially agree. I love the ability to skip to the next iteration (it's available in the mainframe language that I use) and really miss it in VB. But MS's workaround sounds really ugly.

          I'd like to know which "previous versions of VB" they're referring to, as VB6 certainly doesn't have a Continue statement.

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            Originally posted by Killer42
            I partially agree. I love the ability to skip to the next iteration (it's available in the mainframe language that I use) and really miss it in VB. But MS's workaround sounds really ugly.

            I'd like to know which "previous versions of VB" they're referring to, as VB6 certainly doesn't have a Continue statement.
            Those were my thoughts exactly, because I'd tried in VBA and it didn't work there either.

            Comment

            • humilulo
              New Member
              • Mar 2013
              • 1

              #7
              i don't know where you all get your (out-dated) info. at http://msdn.microsoft.com/en-us/libr...(v=vs.80).aspx you can find the VB.NET 'continue' statement which uses an additional 'do', 'for', or 'while':
              "Continue For" for the C# equivalent of 'continue' in a for loop.

              It was added in Visual Basic 2005 (VB 8). reference: http://en.wikipedia.org/wiki/Visual_Basic_.NET
              Last edited by humilulo; Mar 1 '13, 02:42 PM. Reason: more details on when VB "continue" was added to VB.

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #8
                Well, I think the basic problem here is that the question was asked in the pre-dot-net VB forum. Someone (such as myself) really should have directed the poster to the VB.Net forum for more up-to-date info.

                Of course this may all be a little late, given this thread is about 6 years old.

                Comment

                Working...