coderex
coderex

Reputation: 27885

ActionScript 3: Array length issue

HI this my code, am unable to get the length of the array.

package com.mypro {


    public class MyEve {

        public var process:Array ;


        public function MyEve() {
            // constructor code
            this.process = new Array();
            trace("this.process " + this.process)
            this.process["CONFIG_XML_LOADING"]          = 'Configuration XML Loading';
            this.process['CONFIG_XML_LOADED']           = 'Configuration XML Loaded';


            trace("this.process +++ " + this.process.length)

        }

    }
}

OutPut

// Trace
this.process 
this.process +++ 0

Upvotes: 0

Views: 2371

Answers (3)

Max Dohme
Max Dohme

Reputation: 700

You should use an Object to create an associative array.

public var process:Object;
public function MyEve() {
    // constructor code
    this.process = new Object();
    trace("this.process " + this.process)
    this.process["CONFIG_XML_LOADING"]          = 'Configuration XML Loading';
    this.process['CONFIG_XML_LOADED']           = 'Configuration XML Loaded';


    trace("this.process +++ " + this.process.length)

}

Read this for more information.


EDIT: Ah, yes. As divillysausages pointed out you were trying to get the length. That isn't directly possible for Objects. You can, however, iterate through an Object's properties with the for...in loop, making it trivial to count each property of the object. Here and here are good examples of how to use it.

Upvotes: 1

divillysausages
divillysausages

Reputation: 8053

While it's possible to use an array as a associative array, the length property won't work - it'll just return 0. You need to calculate the length yourself using either a for..in or a for each:

for..in:

var len:int = 0;
for ( var key:String in this.process )
    len++;

for each:

var len:int = 0;
for each( var value:String in this.process )
    len++;

as pointed out before though, it's probably better if you use Object (String key) or Dictionary (Object key) for this sort of thing.

Upvotes: 4

Eugene
Eugene

Reputation: 732

See [] array access from AS3 Reference.

You must use integer value for arrays or instead Array use Object or Dictionary

Upvotes: 0

Related Questions