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: Jonathan Johnson <jonj at realsoftware dot com>
Date: Wed, 30 Nov 2005 19:26:11 -0600
Delivered-to: realbasic-nug at lists dot realsoftware dot com
Domainkey-signature: a=rsa-sha1; q=dns; c=nofws; s=beta; d=gmail.com; h=received:message-id:date:from:sender:to:subject:in-reply-to:mime-version:content-type:content-transfer-encoding:content-disposition:references; b=omH7Y0tTDw7iVdQ0A1t2aw0/AmYLF9sxiN0+6DsHE7bpkq/K8d/CGTKPyxoYEiKIPSkg2clNQB1qOZHvrxK/VU7AQfJo1OoM8SJ/DpiFdb+gR0+GW8Kmd4TWdhukxL9meptxRYvDDNGGHbGDMcc4/sWJFiy4KbA+Ti9MDFch/LE=
References: <20051130194514 dot 26134EDC8E7 at lists dot realsoftware dot com> <p06210276bfb3ce64512e at 192 dot 168 dot 1 dot 4>
On 11/30/05, Robert Woodhead <trebor at animeigo dot com> wrote:
> 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.

All array operations will throw an OutOfBounds exception if you pass
in an index that is invalid.

> 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

Why not just:

while n >= 0 and n <= ubound( myArray )
  dedorkify(myArray(n))
  n = myArray.IndexOf( dork, n+1 )
wend

--
Jonathan Johnson
REAL Software, Inc.

REAL World 2006, The REALbasic User's Conference
<http://www.realsoftware.com/realworld>
_______________________________________________
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>