On 2005-04-11, Herfried K. Wagner [MVP] <hi***************@gmx.at> wrote:
"David" <df*****@woofix.local.dom> schrieb: Dim items As New ArrayList()
For i As Integer = 0 To 4
That introduces an off-by-one error. The first loop runs 4 times, your
replacement runs 5 times.
ACK, maybe I was too busy to fully concentrate on what I type when writing
the reply.
The fact that this is hard to see argues
pretty strongly against using a For loop here, IMHO.
I still think that 'For...To' is the better solution in this case, at least
from what we know about it.
Given the complete context of the final solution, incrementing an
counter over each index of a collection, I'd agree. But I'm curious
what you'd really choose for the problem as presented originally:
execute a series of commands some specific number of times. In
Mcconnellesque detail, say we have a retry count count...
For i = 0 to MaxAttempts
is wrong, as we've seen.
For i = 1 to MaxAttempts
just feels wrong to me. my mind is zero-based, I can't help it. And
if I ever do need the index, I haven't got it.
For i = 0 to MaxAttempts - 1
That's not so bad, and I pretty much consider it a standard VB.net
idiom. However, I was very uncomfortable with it when I first
started on VB.Net.
[Do] While i < MaxAttempts
... do stuff
i += 1
Loop
This feels pretty good to me also, although separating the increment
might bother some.
I lean toward #3, but the fact that #1 is so error-prone and so similar
to #3 makes me worry about it a bit.
Of course, it also occurs to me that I very seldom have loops like this.
It seems I'm usually dealing with the length or count of something.
David