Why doesn't this simple For Next loop work??

FastPlymouth

Member
Joined
Jan 26, 2006
Messages
8
Hey All,

Ive racked my brain for 2 days trying to figure this one out...

I have a button event handler that loops through a repeater comparing textbox values within each row to the corresponding row in a datatable.

As far as I can tell all of the code inside my loop is fine, but for some reason the loop would only execute for every other iteration. Puzzled, I stripped the loop down until it was nothing but the following:
Code:
Dim i As Integer = 0
Dim debug As String
For i = 0 To 6 Step 1
     debug = debug & "," & i
      i = i + 1
Next
lblError.Text = debug

Notice that I even added the "step 1" just to be sure. My output is ",0,2,4,6".
Am I missing something here!??!? Ive been coding vb.net for well over a year now - is it possible that I somehow hamfisted a basic construct?

Thinking that the problem had something to do with the event handler itself (longshot) I moved the above code into my PageLoad... same thing!!

Does anyone have ANY idea what could cause such a ridiculous condition? I have a feeling its something stupid and easy that I forgot - if this is the case Ill never be able to show my face here again :)

Thanks!
K
 
This line is what is causing the issue.
Code:
i = i + 1

Try something like this. The For...Next Loop automatically increments your variable for you, defaulting to positive one.
Code:
For i as Integer = 0 to 6
    Console.WriteLine(i.ToString())
Next i

The above code just has the wrong type of Loop around it.

Code:
Dim i as Integer = 0
Do While i < 7
    Console.WriteLine(i.ToString())
    i += 1
Loop
 
Last edited by a moderator:
Back
Top