PDA

View Full Version : How Do I: initialize an array


Jerry62712
08-07-2009, 03:38 PM
I have the following code:
[Bindable]
public var pageDropDownUtility:Array = new Array("None",
"Receiving or expecting to receive energy benefits from LIHEAP",
"Billed for AC/Heat",
"Billed for more than 1 utility excluding AC/Heat",
"Billed for 1 utility excluding AC/Heat and telephone",
"Billed for telephone only");


I would rather load this data from this class:

private const dropDownUtility:Array = new Array([
"None",
"Receiving or expecting to receive energy benefits from LIHEAP",
"Billed for AC/Heat",
"Billed for more than 1 utility excluding AC/Heat",
"Billed for 1 utility excluding AC/Heat and telephone",
"Billed for telephone only"]);
public function getDropDownUtility():Array {
return dropDownUtility; }

How do I do it? I've loaded all the strings with no problem:

code:
[Bindable] public var pageButtonLanguage:String = English.getLanguage();

and

class:
private const language:String = new String("Espaņol");
public function getLanguage():String {
return language; }

wvxvw
08-07-2009, 04:13 PM
var foo:Array = [];
// and
var bar:Array = new Array();
Are very similar, but:
- [] is faster
new Array() if given 1 integer as an argument will create an empty array of that integer value's length.

It is advised that you never use new Array(), but in the latter case only.

At the same note:
new String();
Doesn't make sense in AS3 because built-in classes don't really have constructors. Even though this syntax is valid it is redundant. You should do:
var s:String;
or
var s:String = "initializer";

Another thing: it is always better to write a bindable getter/setter then just declare a variable and put there a [Bindable] meta - the autogenerated code is always worse then the one, you'd write yourself.

Jerry62712
08-07-2009, 08:33 PM
Thanks for the input. I'll change the classes to eliminate the String constructor.

I still don't see how to load the array with the data from the class. I want to do this to eliminate the hard coded stuff and let it all come from the class.

Please elabroate on the binding of the getter. I've never seen this.

wvxvw
08-08-2009, 02:21 AM
As for array... you must be trying to recreate something possible in another programming language, which isn't possible in AS3 :) because I don't really understand what do you want to do, when you say "load array". You may either have an initializer of type array, or just create an empty array - [] and then use Array#push(), Array#unshift() and all other array;s methods to populate it...

The example of bindable getter/setter:

[Bindable("xChange")]

/**
* ...
* This property can be used as the source for data binding.
* When this property is modified, it dispatches the <code>xChange</code> event.
*/
override public function get x():Number { return _transformMatrix.tx; }

override public function set x(value:Number):void
{
if (_transformMatrix.tx == value) return;
_transformMatrix.tx = value;
invalidLayout = true;
dispatchEvent(new Event("xChange"));
}
When compiler sees the [Bindable] meta on property it generates similar get/set pair, but it won't check if a new value is different from the old one for example, thus firing redundant change event, besides it will create an ugly name for the generated getter/setter.
Another difference is that compiler will always generate public property (which you'd do for the getter/setter in most cases anyway), but if you'd like to reference your "simple" bindable property from outside the class, you'd have to have 2 public definitions - this means more bytecode for the class -> more downloading time etc.