views:

34

answers:

3

Hello there, I'm wondering if is there any way to mimic the same behaviour we have for top level classes in AS3 for example:

var myArray:Array = [1,2,3,4];
trace(myArray) // [1,2,3,4];

As you can see it returns the own object when tracing it.
but if I create my own class that extends Array I get

var queue:Queue = new Queue([1,2,3,4]);
trace(queue) // no output

so there are 2 questions here.

is it possible to create a custom class instance just like I create an Array like:

var queue:Queue = [1,2,3,4];
//instead
var queue:Queue = new Queue([1,2,3,4]);

and how can I return the super object when asking for the object like;

trace(queue) // [1,2,3,4];

I'm not sure if this is possible to do in AS3

thanks for you help

+1  A: 

According to livedocs.adobe.com,

"You can extend the Array class and override or add methods. However, you must specify the subclass as dynamic or you will lose the ability to store data in an array."

Perhaps you're not explicitly specifying the subclass as dynamic (declared as public dynamic class Queue)?

Robusto
Hey Robusto thanks a lot for you help this is basically the beginning of everything :) Jamie posted his question above where I could find the answer. thanks for your help
ludicco
A: 

@jamie-wong pointed the right answer replied by @quasimondo right here.

This isn't an answer of mine but for @quasimondo, but since there's no way to classify a comment as the right answer I'd prefer to have this question replied instead. All credits go for both of them.

ludicco
A: 

The best way to do this is to override the toString method of the class which can be made to output whatever you want when you concatenate the class with a string or just trace it.

public class MyClass
{
    private var _values:Array = [1,2,3,4];
    .
    .
    . 
    override public function toString():String
    {
        return 'MyClass[' + _values.join(',') + ']';
    }
}

var mc:MyClass = new MyClass();
trace(mc);

Which would output:

MyClass[1,2,3,4]
Rudisimo