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.






















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?
Posted by: Peter | October 16, 2008 at 02:34 AM
Really nice ! Again something I didn't know... :-)
Posted by: Cyril Hanquez | October 16, 2008 at 08:55 AM
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.
Posted by: Gareth Arch | October 16, 2008 at 10:21 PM
Gareth,
Are you using MXML compiler right ?
Otherwise no errors will be thrown.
Best
Posted by: Marco Casario | October 22, 2008 at 06:54 PM
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.
Posted by: Mário Júnior | October 28, 2008 at 07:38 PM
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.
Posted by: Ricardo Poblete | November 03, 2008 at 07:48 PM