My Books

  • Follow me on Twitter
My Photo

Subscribe my blog

  • Get this widget from Widgetbox
  • Add to Google
  • RSS FEEDS
  • Enter your Email here to subscribe :



    Powered by FeedBlitz

Blog Widget by LinkWithin

« Want to join the beta of the next Flash Media Server 4 ? | Main | Heading to The ActionScript Conference in singapore »

Specifying an ActionScript Value Object as data type of the elements of an Array using the [ArrayElementType]

It's a fact that ActionScript does not support typed arrays. It means that you can't specify the data type of the elements of the Array. Flex SDK provides the [ArrayElementType] meta tag that allows you to specify Array type checking using Flex Builder and the Flex compiler:

[ArrayElementType("int")]  private var _intArray:Array;

[ArrayElementType("String")] private var _stringArray:Array;

In the above example I've specified the data type for the Array elements. In the first Array,_intArray , the elements of the Array must be int, while in the second Array, _stringArray, the elements of the Array must be String. If you try to put different data types into the Array the Flex compiler will issue a syntax error.  Cool and simple.

But you can also use the [ArrayElementType] to specify an ActionScript Value Object (it's an ActionScript class) as data type of the elements of the Array. In this scenario you pass the fully qualified package reference of the ActionScript Value Object as type of the ArrayElementType:

[ArrayElementType("com.casario.vo.UserVO")]
protected var _users:Array;

Then you can populate the _users property using, for istance, the push method:

_users.push( new com.casario.vo.UserVO ( id, name, role, description ) );

The ActionScript Value Object should look like this:

package com.casario.vo
{
    [Bindable]
    public class UserVO
    {
        public var id:String;
        public var name:String;
        public var role:String;
        public var description:String;

        public function UserVO(
                                id:String,
                                name:String,
                                role:String,
                                description:String,
                              )
        {
            this.id = id;
            this.name = name;
            this.role = role;
            this.description = description;

        }
    }
}

Forcing the value object as data type you'll get compile-time errors if you'll attempt to assign elements of a data type other than the UserVO ActionScript Value Object to the Array in an MXML file.

Comments

That's a very useful tip, thanks Marco! I suppose we'll be able to do something similar to this approach in Gumbo using Vectors if targeting Flash Player 10, which essentially are mono-typed arrays?

Really nice ! Again something I didn't know... :-)

Is there something special that needs to be add to the compiler arguments? I just tried setting an incompatible VO to my array, and no error was thrown in compile-time nor run-time. I, at first, set it inside an AS object (such as a TestVO) with a property that was an array (typed to TestUser). I tried pushing a different object into the array (something other than TestUser) and no error was thrown. I also tried setting a typed array in my main application file and pushing a non-TestUser object into it, but it still allowed it (with no error thrown). Is there something else I need to do to get this to work properly?
Thanks.

Gareth,
Are you using MXML compiler right ?
Otherwise no errors will be thrown.
Best

Just, remembering wich meta-tag made tha compare type in compile time. In runtime, not compares the object type has been inserted.

This feature is usefull for who build custom components.

In the Flash Player 10, we have the Vector for simulate the Generics Java/.Net concept. In this case, is able use: protected var _users:Vector;

Regards.

I have no error was thrown in compile-time, i tried on FLex2 and Flex3. This is my code.

import VO.UserVO;
ArrayElementType("UserVO")] protected var _users:Array = new Array();

private function load():void{
_users.push( new UserVO ('1', 'ripoblet', 'role', 'description' ) );
_users.push(1,2,3,4);
}

Maybe i need some arguments.

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Pistach.io

Speaker at

WebDeveloper's Journal Author

  • My favourite Flash Lite 3 mobile phone
  • Web Developer's & Designer's Journal by Sys Con Media
  • Web Developer's & Designer's Journal Blogger
    Web Developer's & Designer's Journal by Sys Con Media
  • FullAsGoog Aggregator
  • Macromedia WebLogs Aggregator

July 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31