realbasic-nug
[Top] [All Lists]

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

To: realbasic-nug at lists dot realsoftware dot com
Subject: Array.indexOf(value,startingIndex) has a gotcha!
From: Robert Woodhead <trebor at animeigo dot com>
Date: Wed, 30 Nov 2005 16:55:21 -0500
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <20051130194514 dot 26134EDC8E7 at lists dot realsoftware dot com>
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.


--

Woodhead's Law: "The further you are from your server,  the more likely
it is to crash."
_______________________________________________
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>