Q: For Each loops

  • Thread starter Thread starter Geoff Jones
  • Start date Start date
G

Geoff Jones

Hi

Suppose I have a For Each loop, for example,

For Each x As DataRow In drMyData
' Do stuff - calculation 1
' Do more stuff - calculation 2
' and even more stuff - calculation 3
Next

If after completing calculation 1, I discover that there is no point doing
calculations 2 and 3, how do I jump back to the start to starting processing
the next DataRow?

I've tried adding a conditional statement something like:

If (no point carrying on) Then
Next
End if

but I can't get it to compile.

Can anybody please help?

Thanks in advance

Geoff
 
Simply structure your code differently with conditional branching such as
select case and if's, if you cant cope with that then you could put a goto
statement in, but I try to avoid this if possible

if then goto continue


continue:

Next

--

OHM ( Terry Burns )
. . . One-Handed-Man . . .

Time flies when you don't know what you're doing
 
why not just do this?
For Each x As DataRow In drMyData
' Do stuff - calculation 1

' Do more stuff - calculation 2
' and even more stuff - calculation 3
 
not just do this?
For Each x As DataRow In drMyData
' Do stuff - calculation 1
if calculation1ok then
' Do more stuff - calculation 2
if calculation2ok then
' and even more stuff - calculation 3
end if
end if
Next

hth
guy
 
Well, it is a FOR loop, how about using LOOP ?
If you want to exit and stop processing any more rows, use EXIT
 
Many thanks for all your suggestions.

Geoff

PAul Maskens said:
Well, it is a FOR loop, how about using LOOP ?
If you want to exit and stop processing any more rows, use EXIT
 
try
do stuff1
if thatsall then exit try
do stuff2
if thatsall then exit try
do stuff3
catch
end try
 
<face colour=RED>
I should check the language I'm posting in.
LOOP and EXIT aren't inVB.
</face>

Wrapping the Calculation2 and Calculation3 in an IF statement would be one
way to prevent execution.

Unless you set a "No Point Doing Calculations" flag in Calculation1.
Then you can test it in Calculation2 and Calculation3 and immediately return
having done nothing.

There are arguments for and against both approaches.
I'd lean towards an IF statement in the loop, because the program flow is
clear.
Once you hide program flow by Calculation2() being able to do nothing as
well as the calculation, then it maikes maintenance more dififcult.
 
If after completing calculation 1, I discover that there is no point doing
calculations 2 and 3, how do I jump back to the start to starting processing
the next DataRow?

For what it's worth, in VB 2005 we'll FINALLY get the Continue statement.
For now, if it's a simple test, just enclose calculations 2 and 3 in an If
block that will get skipped if calculation 1 is "pointless." If it's some
massively convoluted nested-If scenario, I personally think GoTo, like OMH
mentioned, is the cleanest way.
 
Back
Top