Continue statement in VB.NET 2005

Posted Sat, Jun 19 2004 3:59 by bill

Mike McIntyre shows some examples of using the Continue statement in VB.NET 2005. Another cool feature of the Continue statement is it allows for the Continue to be on an outer loop. For example, if you were iterating over items in a 2 dimensional jagged array, you can use code like this:

  Dim values()() As Int32
  Dim i As Int32 = -1

  While i < values.Length
     i += 1
     For j As Int32 = 0 To values(i).Length - 1
        If values(i)(j) = 2 Then Continue While
        ' Some lengthy operation
     Next j
     ' more lengthy operations
  End While

Note that the continue statement in the above code is Continue While, not Continue For. The caveat is, you have to have different loop types.  Thankfully VB.NET has three loop constructs, Do, While and For, so you can mix and match these pretty easily.  And intellisense is pretty cool here as it lists the loops you can use Continue on based on your actual code.  In the above example when you type “Continue ” intellisense lists For and While.

Personally, I would like the VB team to add to that for the sake of For loop. That is I *wish* I could do this:

   For iAs Int32 = 0 To values.Length - 1
      For j As Int32 = 0 To values(i).Length - 1
         If values(i)(j) = 2 Then Continue For i
         'Some lengthy operation
      Next j
      'more lengthy operations
   Next i

The difference being I could specify “Continue For i” or “Continue For j”.

Still, Continue is a good and overdue addition to VB. Great to have it at last !!

 

Filed under: ,

Comments

# re: Continue statement in VB.NET 2005

Saturday, June 19, 2004 5:15 AM by bill

Bah! Who needs Continue when you have GoTo? ;-)

# re: VB.NET Wish

Tuesday, July 13, 2004 6:37 PM by TrackBack