ArrayIsDefined throws CF error

Posted At : February 20, 2008 11:02 AM | Posted By : Dave
Related Categories: ColdFusion 8

Another in the long list of new things in ColdFusion 8 is a new function called ArrayIsDefined. This will allow you to check to see if an array element is defined.

However, I ran into an issue with it that seems kind of odd. If your array has 5 elements and you try to see if array element 6 is defined ColdFusion will throw an error.

Example:

<CFLOOP INDEX="i" FROM="1" TO="5">
   <CFSET testArray[i] = i>
</CFLOOP>


<CFOUTPUT>
   #arrayLen(testArray)#<BR>
   #ArrayIsDefined(testArray, 6)#
</CFOUTPUT>

Error: Cannot access array element at position 6. The array has 5 indexes. Valid positions are from 1 to 5.

The error states that the array has a length of 5 and it can't access element 6. Seems a little strange that the CF error tells me what the function should have.

The only work around is to check the length of the array before you check to see if the element exists. Hopefully this will be fixed in a future release.

Till next time...

--Dave

Comments
I'm working on a bug surrounding array elements that aren't defined and came up with the same solution of checking the arrayLen(). After checking the LiveDocs for this function's full definition, I would expect this function to return _false_ when you've asked for an element beyond the length of the array.

http://livedocs.adobe.com/coldfusion/8/htmldocs/he...

It's like this is what's happening:

cfset foo = arrayNew(1);
cfset foo[1] = "a";
cfset foo[3] = "c";

arrayIsDefined(foo, 2) ~ Oh, you seem to have missed defining array element 2, FALSE

arrayIsDefined(foo, 4) ~ I don't seem to understand what you're asking here. You stopped at element 3, can't you see that? I mean, element 4? What's element 4? I just don't get it. You're speaking gibberish man. I really don't see ACK! *stroke* CFERROR
# Posted By Adrian J. Moreno | 2/20/08 1:42 PM
Ugh. Thank you Adobe for making me hit my head on the wall thinking it was I who was doing something wrong. By definition this "new" function is useless.
# Posted By chief | 3/18/08 6:15 AM