Interesting problem: My For loop doesn't want to loop!

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am writing a program in VB.NET and as I was debugging a problem I noticed my For loop doesn't want to loop!
I originally had a upper bound which was an expression and it wasn't working. WHen I noticed this I changed the upper bound for a simple digit. As I step through my code, I see that it simply goes through the loop once and continues on it's merry way. Now I have programmed in Java, C++, and even Modula 3 before and I have never seen this EVER!
Do I have something wrong with my settings or what? I'm really stumped!
H E L P!!

here's the code:

dim i as Integer
For i = 0 To i = 5
'objdsBook is a dataset
output = output & objdsBook.BookCondition(i).Item(0).ToString
Next i
 
Wouldn't it be

Dim i as Integer

For i = 0 To 5
'objdsBook is a dataset
output = output & objdsBook.BookCondition(i).Item(0).ToString
Next i

Not, i = 0 To i = 5

Rudy said:
I am writing a program in VB.NET and as I was debugging a problem I
noticed my For loop doesn't want to loop!
I originally had a upper bound which was an expression and it wasn't
working. WHen I noticed this I changed the upper bound for a simple digit.
As I step through my code, I see that it simply goes through the loop once
and continues on it's merry way. Now I have programmed in Java, C++, and
even Modula 3 before and I have never seen this EVER!
 
Ok, thanks Mark, that was it.
Man I am soooo embarrased. What a simple mistake.

Thanks a million!
 
Rudy,
You may want to consider using Option Strict On, you would have received a
compile error to the effect "Option Strict On disallows implicit conversions
from 'boolean' to 'integer' on the expression "i = 5" on the For statement.
I find compile errors easier to fix, then obscure runtime behavior.
Unfortunately Option Strict On requires you to be more specific in your code
(requiring use of CType & DirectCast more) as it disallows late binding &
some implicit conversions. However this generally means you code may execute
faster...

I would also recommend you consider using a StringBuilder instead of string
concatenation.
dim i as Integer
Dim sb As New System.Text.StringBuilder
For i = 0 To 5
'objdsBook is a dataset
sb.Append(objdsBook.BookCondition(i).Item(0))

Next i
output = sb.ToString()

For 6 rows using StringBuilder may not make much of a difference, however
for 600 rows I'm sure it will.

Hope this helps
Jay

Rudy said:
I am writing a program in VB.NET and as I was debugging a problem I
noticed my For loop doesn't want to loop!
I originally had a upper bound which was an expression and it wasn't
working. WHen I noticed this I changed the upper bound for a simple digit.
As I step through my code, I see that it simply goes through the loop once
and continues on it's merry way. Now I have programmed in Java, C++, and
even Modula 3 before and I have never seen this EVER!
 
Back
Top