For Next loop skipping for...

dgorka

Well-known member
Joined
Dec 27, 2006
Messages
88
Programming Experience
5-10
Hello all.

I have a For Next loop that seems to skip my for statement. Basically, I'm using nested for loops to go through a database and grab various information about some TIFF images. By the time I get to looping though the paths of the TIFFs (code below), it hits the problem:

VB.NET:
'Now put the tiffs in proper format for output
For Each drTiff As DataRow In ds.Tables("TiffImages").Rows
    'Set the tiff# for the Fetch Format
    tiffNumber = tiffNumber + 1

    'make sure that the tiff number doesn't exceed 999
    If tiffNumber > 999 Then
        tiffNumber = 1
        DocTypeNumber = DocTypeNumber + 1
    End If

    'Call the sub to do the formatting
    FormatTiffs(drTiff.Item(0).ToString, drTiff.Item(1).ToString, _
tiffNumber, DocTypeNumber, drBatch.Item(0).ToString, drLoan.Item(0).ToString, drDocType.Item(1).ToString)
Next ' drTiff

It hits the for when it initially enters the loop, it increments my counter, checks the tiff number, then formats the images how i need them. Then it hits the next. I have a breakpoint on both the For and the Next. If I step through on the Next, it skips the For and goes to tiffNumber = tiffNumber + 1.

At first I thought maybe it wasn't showing me that line, which is why I put a breakpoint on both. What i've found that its doing is, say my datatable has 8 rows in it, it will loop through all 8 rows of images, doing what I want, then it will hit the For and do it again, until its hit all 8 rows. So instead of ending with me having 8 images copied to my output folder and formatted, I have 64 images copied to my output folder and formatted.

For the life of me I can't figure out why this is happening. Has this happened to anyone else? And if so how did you correct it...if possible. Thank you all for reading this and for your time.
 
that wont make a difference. It is legal to declare the temp loop enum variable in the loop decl:

For Each var As Type In EnumerableCollection

That's legal.

The only thing I can think, dgorka, for to explain your behaviour that you see, is where you have inadvertently started 8 threads. They all come to rest on the FOR breakpoint, you start, and then the first one breaks on the next point (tif +1)
Then you start, and the NEXT thread also comes to rest on this line
etc..

i.e. if you have multiple threads running and youre debugging, the IDE will swithc between ALL threads. To help with what is going on, you should:

Always use Thread.CurrentThread.Name = "something" when you start a new thread, to name it. Try to name your threads something differntly, obviously. Like:
Thread.CurrentThread.Name = "Processing" & filename
You can only set a thread name once!

Also, use the THREADS debug window to look at all the running and stopped threads. Pay note to the thread ID, if the current thread ID is changing it means youre looking at what another thread is doing,.

There is nothing wrong with your for loop, and I cant really think of another explanation. If this isnt the problem, please pos more code outside the loop.. i,e, post all code since the program start or user interaction, that started this action.
 
Nested For loops...? If you do this:
For Each drTiff0 As DataRow In ds.Tables("TiffImages").Rows
For Each drTiff As DataRow In ds.Tables("TiffImages").Rows
'format it
Next
Next
...then the inner loop will result in a multiple of the number of items, ie 8 items gets formatted 64 times.
 
Nested For loops...? If you do this:
For Each drTiff0 As DataRow In ds.Tables("TiffImages").Rows
For Each drTiff As DataRow In ds.Tables("TiffImages").Rows
'format it
Next
Next
...then the inner loop will result in a multiple of the number of items, ie 8 items gets formatted 64 times.

Mmmhh.. But I couldnt use that to explain how the IDE seemed to skip over the breakpoint on the FOR, and land on the x+1 line 8 times, then stop on the for.. All in, its a bit weird. We definitely need more code..
 
Okay, I've figured it out.

So each of my loops is a For whatever as DataRow in ds.Tables("WhateverTableItIs").Rows, The only problem is I was never clearing my tables after I left the loop... So then it would just append the new data onto the table, thats why it seemed to be doubling my data.

I had basically the same code in another project, and it worked fine there and couldn't figure out why it didn't work here, so I looked at that, still couldn't get it. So then I copied the other code into this project and started editing it to work here, it was then that I noticed the clears in that one, but not this. So yeah.

Thank you for taking the time to look at this and help me figure it out. I did actually watch the threads as I debugged it and it stayed on the same thread, so I figured it had to be something else. Again, thank you.
 
Back
Top