By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,937 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

vb.net equivalent to c# continue

P: 1
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.
May 8 '07 #1
Share this Question
Share on Google+
7 Replies


SammyB
Expert 100+
P: 807
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
Expand|Select|Wrap|Line Numbers
  1.         Dim j As Integer
  2.         For i As Integer = 1 To 4
  3.             If i = 3 Then Exit For
  4.             j = i
  5.         Next i
  6.         MsgBox(j) ' will give 2
  7.  
May 8 '07 #2

Expert 5K+
P: 8,434
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...
Expand|Select|Wrap|Line Numbers
  1. For Index = 1 to 10
  2.   Do
  3.   If some condition Then
  4.     Exit Do ' Equivalent of applying Continue to the For loop.
  5.   End If
  6.   ... other processing that you wanted to skip ...
  7.  
  8.   Loop While False ' In other words, don't loop.
  9. Next
It's a little ugly, but it works.
May 9 '07 #3

SammyB
Expert 100+
P: 807
> 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.
May 10 '07 #4

Expert 5K+
P: 8,434
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.
May 10 '07 #5

SammyB
Expert 100+
P: 807
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.
May 11 '07 #6

P: 1
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
Mar 1 '13 #7

Expert 5K+
P: 8,434
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.
Apr 4 '13 #8

Post your reply

Sign in to post your reply or Sign up for a free account.