469,315 Members | 1,510 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

For Next loop & looping early

How do I loop back to the beginning of a for/next loop before getting to
the end of it? Isn't there an "iterate" command or something like that?

For Each This in That
...code
if This = False Then (start loop over with next This)
...code
Next

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #1
8 2512
* Terry Olsen <to******@hotmail.com> scripsit:
How do I loop back to the beginning of a for/next loop before getting to
the end of it? Isn't there an "iterate" command or something like that?


Not in VB.NET 2003. You will have to use a label + 'GoTo', or prevent a
block inside the loop from being executed using 'If...Then'. In VB
2005, 'Continue' is available.

--
Herfried K. Wagner [MVP]
<URL:http://dotnet.mvps.org/>
Nov 21 '05 #2
Terry,

What do I miss?

For Each This in That
if This = True Then
...code
End if
Next

Works like a charm, with the exception when you try to delete a This in that
code.

I hope this helps?

Cor
Nov 21 '05 #3
Yeah that works...I just didn't want to put about 50 lines of code inside a
block if statement.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...
Terry,

What do I miss?

For Each This in That
if This = True Then
...code
End if
Next

Works like a charm, with the exception when you try to delete a This in that code.

I hope this helps?

Cor

Nov 21 '05 #4
You can use a try/end try block. Then use exit try to goto the next loop.
Frank

"Terry Olsen" <to******@hotmail.com> wrote in message
news:OQ*************@TK2MSFTNGP09.phx.gbl...
How do I loop back to the beginning of a for/next loop before getting to
the end of it? Isn't there an "iterate" command or something like that?

For Each This in That
...code
if This = False Then (start loop over with next This)
...code
Next

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 21 '05 #5

"Terry Olsen" <to******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
Yeah that works...I just didn't want to put about 50 lines of code inside a block if statement.


There's nothing wrong with that. I have no problem using GoTo in this case
because VB has always lacked a Continue statement (wait a year!), but I only
use it in cases where I'm in a deeply nested loop and only one convoluted
condition requires me to iterate immediately. Otherwise I kinda feel
dirty....
Nov 21 '05 #6
Terry,
50 lines sounds like you need a new method! :-|

I would put the 50 lines in its own method (Sub or Function) then the For
Each can have a simply If that simply calls the method.

Then again I tend to follow OO with smaller objects with smaller methods...

Hope this helps
Jay
"Terry Olsen" <to******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
Yeah that works...I just didn't want to put about 50 lines of code inside a block if statement.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...
Terry,

What do I miss?

For Each This in That
if This = True Then
...code
End if
Next

Works like a charm, with the exception when you try to delete a This in

that
code.

I hope this helps?

Cor


Nov 21 '05 #7
What's the largest method you have coded? Believe me, I've split up my
program into Mods & Functions & Methods. But it seems as though I'm slowing
it down quite a bit with all the "jumping around". The meat of the program
is a loop. It goes out to about 30 servers (some on 56k and some on 128k so
threading wouldn't help me here), downloads 30-100 log files from each
server (after telling the server to zip them up), then it unzips & parses
all those files to create an html page that displays the data from the logs
in an easily readable format. I tried having a method called
"WriteLineToFile", but having the code inline seems waaaay faster...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Od**************@TK2MSFTNGP10.phx.gbl...
Terry,
50 lines sounds like you need a new method! :-|

I would put the 50 lines in its own method (Sub or Function) then the For
Each can have a simply If that simply calls the method.

Then again I tend to follow OO with smaller objects with smaller methods...
Hope this helps
Jay
"Terry Olsen" <to******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
Yeah that works...I just didn't want to put about 50 lines of code inside
a
block if statement.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...
Terry,

What do I miss?

For Each This in That
if This = True Then
...code
End if
Next

Works like a charm, with the exception when you try to delete a This

in that
code.

I hope this helps?

Cor



Nov 21 '05 #8
Terry,
What's the largest method you have coded? Does it really matter? Size doesn't really matter here, the point I was
making is that the "length" of your method is really in the second method,
the first method is simply the loop & possibly the if. The second method has
all the code for the loop itself, allowing the second method to use Exit Sub
to go to the top (bottom really) of the loop...
But it seems as though I'm slowing
it down quite a bit with all the "jumping around". Define "seems", I would use CLR Profiler or other profiling techniques to
actually time the loop & subroutines to see how will it performs or not
performs.

Remember the 80/20 rule (link below) that is 80% of the execution time of
your program is spent in 20% of your code. I will optimize (worry about
performance) the 20% once that 20% has been identified & proven to be a
performance problem via profiling (CLR Profiler is one profiling tool).
It goes out to about 30 servers (some on 56k and some on 128k)
downloads 30-100 log files from each
server (after telling the server to zip them up), then it unzips & parses
all those files to create an html page that displays the data from the logs
Sounds like the ideal candidate for multi-threading!

As I would expect there is a lag connecting to each server, plus a lag
having each server zip its files, then another lag in downloading the files.
If you save the html pages to disk, there would be a lag there.
Multi-threading would allow your processor to work on another request while
its waiting for the lag...

I would define the routine that processed a single server as a subroutine, I
would then use System.Threading.ThreadPool.QueueUserWorkItem to queue each
request, to this subroutine. Where the Request includes a "State" object
with the server to process & other request specific info. This way a couple
of 128K servers will finish for each 56K server

Unless of course you have a single modem that is dialing each server, then I
would single thread the "download" part & consider multi-threading the unzip
& parse part...
For info on the 80/20 rule & optimizing only the 20% see Martin Fowler's
article "Yet Another Optimization Article" at
http://martinfowler.com/ieeeSoftware...timization.pdf

Hope this helps
Jay

"Terry Olsen" <to******@hotmail.com> wrote in message
news:uV**************@TK2MSFTNGP12.phx.gbl... What's the largest method you have coded? Believe me, I've split up my
program into Mods & Functions & Methods. But it seems as though I'm slowing it down quite a bit with all the "jumping around". The meat of the program is a loop. It goes out to about 30 servers (some on 56k and some on 128k so threading wouldn't help me here), downloads 30-100 log files from each
server (after telling the server to zip them up), then it unzips & parses
all those files to create an html page that displays the data from the logs in an easily readable format. I tried having a method called
"WriteLineToFile", but having the code inline seems waaaay faster...
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Od**************@TK2MSFTNGP10.phx.gbl...
Terry,
50 lines sounds like you need a new method! :-|

I would put the 50 lines in its own method (Sub or Function) then the For
Each can have a simply If that simply calls the method.

Then again I tend to follow OO with smaller objects with smaller

methods...

Hope this helps
Jay
"Terry Olsen" <to******@hotmail.com> wrote in message
news:uS**************@TK2MSFTNGP10.phx.gbl...
Yeah that works...I just didn't want to put about 50 lines of code

inside
a
block if statement.
"Cor Ligthert" <no**********@planet.nl> wrote in message
news:ea**************@TK2MSFTNGP12.phx.gbl...
> Terry,
>
> What do I miss?
>
> For Each This in That
> if This = True Then
> ...code
> End if
> Next
>
> Works like a charm, with the exception when you try to delete a This

in that
> code.
>
> I hope this helps?
>
> Cor
>
>



Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Psybar Phreak | last post: by
5 posts views Thread by Petr Bravenec | last post: by
2 posts views Thread by Sparky Arbuckle | last post: by
4 posts views Thread by Slonocode | last post: by
13 posts views Thread by Joseph Garvin | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.