It works properly for me. I get four alerts with "Test this 2", "Test
this -1", "Test this 2", and "Test this -1". By the way, since
Alert.show() simply puts up the alert and returns, they stack up on top
of each other and the topmost alert is from the last call to
Alert.show().

 

You claim that you got s equal to 3. I doubt that... 2 is the correct
answer since indexes are 0-based.

 

By the way, you shouldn't write

 

    var s:int = new int;

 

You should simply write either

 

    var s:int = 0;

 

or

 

    var s:int;

 

Gordon Smith

Adobe Flex SDK Team

 

________________________________

From: [email protected] [mailto:[EMAIL PROTECTED] On
Behalf Of Ramsey, Robert L
Sent: Tuesday, April 15, 2008 2:54 PM
To: [email protected]
Subject: [flexcoders] arrays and indexof()

 

I think I'm being very dumb here, but the indexof() function doesn't
seem to work as advertised. I'm working with Flex3.

Here's a simplified version of the code:

var s:int = new int;
var i:int = new int;
var l:int = new int;
var aYears:Array = new Array("2002", "2003", "2004", "2005");
var o:Object = new Object;
var myYear:int = new int;

myYear = 2004;

o = myYear;
s = aYears.indexOf(o, 0);

Alert.show("Test this " + s.toString()); // here s is -1, showing
that it didn't find the string in the array
s = aYears.indexOf("2004",0);
Alert.show("Test this " + s.toString()); // here s is -1, showing
that it didn't find the string in the array
s = aYears.indexOf(myYear,0);
Alert.show("Test this " + s.toString()); // here s is -1, showing
that it didn't find the string in the array

s = -1;
i = 0;
l = aYears.length;
while (i<l)
{
if (aYears[i] == myYear)
{
s = i;
}

i++;
}

Alert.show("Test this " + s.toString()); // here s is 3, showing
that it did find the string in the array

The help says that indexOf "[s]earches for an item in an array by using
strict equality (===) and returns the index position of the item." 

Can someone tell me how I am being stupid? :)

Thanks,

Bob

 

Reply via email to