GET function returns most correct values, but not all

I have a get function defined as such:


final protected function get data()
:Array
{
  return _array;
}

This function is defined in a parent class, and then used in derrived classes. As you can see, this is a GET function that returns an array. I can do things like this without a problem:
trace(data[‘some_index’]); // give correct value

But these gives me incorrect values:
trace(data); // shows blank (i.e.: Nothing)
trace(data.length); // gives length of 0, even thoght it has members

Is there a way to have those operations return correct values?

Wow, that’s pretty odd. They should be returning the values properly… Depending on what you’re doing with the data, you may want to try returning a clone of _array, rather than the reference to it:

final protected function get data():Array {
return _array.slice(); // leaves _array intact, but returns its value
}

Are you using the array as an associative array? I only ask because your example code has some quotes around it…

Rezmason,
I need to return the array by reference, so I can do this:
data[‘foo’] = ‘bar’; // Which actually works sweet.

Krilnon, Yes, it is an associative array.

Most people discourage using the Array class for associative arrays precisely because you can’t make the length property (and other properties/methods) work. The reference even says so:

Do not use the Array class to create associative arrays (also called hashes), which are data structures that contain named elements instead of numbered elements. To create associative arrays, use the Object class. Although ActionScript permits you to create associative arrays using the Array class, you cannot use any of the Array class methods or properties with associative arrays.

You could also use the Dictionary class depending on what sort of objects you want to use as keys…

Krilnon,

thanks for the info. I did not know that.

With a little updating, my code should work fine, then.

I wonder what reasoning there is behind Adobe allowing Arrays to be used associatively. Is it because Array is a dynamic class, and any dynamic class can be treated like a hash table?

[color=#282827]I agree with you !I support you .[/color]