On Apr 14, 2006, at 7:10 PM, Mark O'Neill wrote:

Hi All,

I've noticed that many snippets of code I've seen use something like:

for i = UBound(MyArray) DownTo 0
...
next

instead of

for i = 0 to UBound(MyArray)
...
next

Now, aside from the obvious that DownTo is in reverse order are there any gains in using DownTo?


Code correctness is sometimes a gain. Suppose you want to loop through an array and remove some elements. Using downto makes it much easier.

For i as Integer = UBound(theArray) downto 0
  If theArray(i)  = "foo" then
    theArray.Remove i
  End if
Next

You can't implement this with a loop in the other direction unless you modify i in the loop, which is considered bad style. Instead you would need to use a While loop --

  dim i as Integer = 0
  While i <= UBound(theArray)
    If theArray(i) = "foo" then
      theArray.Remove i
    Else
      i = i + 1
    End if
  Wend

Charles Yeomans
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to