realbasic-nug
[Top] [All Lists]

Re: Array.indexOf(value,startingIndex) has a gotcha!

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: Array.indexOf(value,startingIndex) has a gotcha!
From: Ryan Dary <nug at ryandary dot com>
Date: Wed, 30 Nov 2005 17:29:45 -0800
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <20051130194514 dot 26134EDC8E7 at lists dot realsoftware dot com> <p06210276bfb3ce64512e at [192 dot 168 dot 1 dot 4]>
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>

<Prev in Thread] Current Thread [Next in Thread>