I disagree. If you are asking the indexOf method to "begin" looking
"after" the last element, then you technically are "outofbounds".
I believe the current behavior is correct. You just need to write your
code differently so that you don't get bit by asking for something that
doesn't exist.
- Ryan Dary
Robert Woodhead wrote:
Consider the following code, that finds all the elements in an array
that equal the value "dork", and processes them:
n = myArray.indexOf(dork)
while (n<>-1)
dedorkify(myArray(n))
n = myArray.indexOf(dork,n+1)
wend
Nice and efficient, right? Quite so. And it will run fine for a long
time, and then bite you in the behind.
Because if the startingIndex of .indexOf is > than the array uBound, RB
throws an OutOfBoundsException. So if the last element in myArray
happens to equal dork, BOOM!
Either this is bad behavior (my opinion is it should return -1; other
languages do this, and IIRC .inStr returns 0 if you go off the end) or
it absolutely needs to be documented in the manual, because it's a total
timebomb.
The workaround is yucky:
n = myArray.indexOf(dork)
while (n<>-1)
dedorkify(myArray(n))
if n<uBound(myArray) then n = myArray.indexOf(dork,n+1) else n=-1
wend
http://www.realsoftware.com/feedback/viewreport.php?reportid=nrwsvjiv
I must say, this app that I'm working on is just the bee's knee's for
finding these kind of things.
_______________________________________________
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>
|