Monday, September 05, 2005

Most common mistake?

Well well, I encountered this before and last friday I made it again!

The mistake is in the for-to-next loop, I've made a loop to loop thru the array with index.

For i as Integer = MyArray.Length to 0
... do something here with MyArray(i) ...
Next

can you see the mistake?

if you can't, you'll probably spend sometime wondering about what's happending and only until you set breakpoints and debug it, you'll found that default the compiler takes it as "Step 1"!

So the fix is as such:
For i as Integer = MyArray.Length to 0 Step -1
... do something here with MyArray(i) ...
Next

regards,
choongseng

No comments: