A Step in the right direction

Posted Thu, Jun 17 2004 4:08 by bill

I was just reading Erik Porter's blog, and he had a code example of :

Dim Count As Integer = 5

For i As Integer = 0 To
Count - 1
    
j = Math.Abs(i - (Count - 1))
Next

And I was thinking why ?? Why not just use VB.NET's Step directive ?

Dim Count As Integer = 5

For i As Integer
4 To 0 Step -1
     j = i
Next

Filed under:

Comments

# re: A Step in the right direction

Thursday, June 17, 2004 4:10 AM by bill

Hey Bill...thanks for readin' my blog ;)

Check out my comment here...sorry I didn't explain it very well...

http://weblogs.asp.net/eporter/archive/2004/06/16/157241.aspx#157275

# re: A Step in the right direction

Thursday, June 17, 2004 4:18 AM by bill

Hey Erik.. Thanks for reading mine ;)

Okay, so you want a counter in both directions, then why not just initiate j outside of the loop and iether increment it or decrement it on each loop.

Now that VB.NET has the continue statement, you might find that a better over-all strategy (just be careful of code placement), as it means you can have one counter running behind the other such as when you are iterating a list and skipping items.

So code such as :
j = -1
For i As Integer = 4 To 0 Step -1
j += 1
Next

or vice versa:

j = 5
For i As Integer = 0 To 4
j -= 1
Next







# re: A Step in the right direction

Thursday, June 17, 2004 4:47 AM by bill

Good point!

It might be interesting to weigh code readability vs. performance, see if either way of doing it would outweigh the other in those two areas.

btw, yah, Continue rocks...I'm so excited for it!

This is why I love blogging I get to share and learn! :)