# GET function returns most correct values, but not all

**URL:** <https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239>\
**Category:** Uncategorized\
**Created:** [May 24, 2008, 6:47pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239 "2008-05-24T18:47:43Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![optimusvlad](https://avatars.discourse-cdn.com/v4/letter/o/5f9b8f/32.png) [@optimusvlad](https://forum.kirupa.com/u/optimusvlad)\
**Post date:** [May 24, 2008, 6:47pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/1 "2008-05-24T18:47:43Z")

</div>

I have a get function defined as such:

```auto

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?

---

<div class="post-metadata">

**Author:** ![Rezmason](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rezmason/32/30447_2.png) [@Rezmason](https://forum.kirupa.com/u/Rezmason)\
**Post date:** [May 24, 2008, 7:04pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/2 "2008-05-24T19:04:45Z")

</div>

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:

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

```

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [May 24, 2008, 7:27pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/3 "2008-05-24T19:27:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![optimusvlad](https://avatars.discourse-cdn.com/v4/letter/o/5f9b8f/32.png) [@optimusvlad](https://forum.kirupa.com/u/optimusvlad)\
**Post date:** [May 24, 2008, 7:48pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/4 "2008-05-24T19:48:07Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![krilnon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/krilnon/32/34_2.png) [@krilnon](https://forum.kirupa.com/u/krilnon)\
**Post date:** [May 24, 2008, 8:47pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/5 "2008-05-24T20:47:17Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![optimusvlad](https://avatars.discourse-cdn.com/v4/letter/o/5f9b8f/32.png) [@optimusvlad](https://forum.kirupa.com/u/optimusvlad)\
**Post date:** [May 24, 2008, 9:39pm UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/6 "2008-05-24T21:39:08Z")

</div>

Krilnon,

thanks for the info. I did not know that.

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

---

<div class="post-metadata">

**Author:** ![Rezmason](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/rezmason/32/30447_2.png) [@Rezmason](https://forum.kirupa.com/u/Rezmason)\
**Post date:** [May 25, 2008, 1:54am UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/7 "2008-05-25T01:54:57Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cjawcrusher512](https://avatars.discourse-cdn.com/v4/letter/c/6f9a4e/32.png) [@cjawcrusher512](https://forum.kirupa.com/u/cjawcrusher512)\
**Post date:** [May 26, 2008, 3:10am UTC](https://forum.kirupa.com/t/get-function-returns-most-correct-values-but-not-all/261239/8 "2008-05-26T03:10:43Z")

</div>

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