开发者

Define unique variable name

How to create a dynamic ArrayCollecton in开发者_C百科stance that use unque naming: ac1, ac2..ac999 whether user click a button. Without having to use hardcode variable name.


You can create dynamically named variables on dynamic classes like this:

package
{
    dynamic public class Test
    {
        public function Test()
        {
            this["a"] = 1;
        }
    }
}

now you could use this class:

var t: Test = new Test();
trace(t.a);

you'll notice that t has a property named "a" and its value is 1.

So using a dynamic class you could create a dynamically named properties in a loop. But I would say this is not better than using a dictionary to save you ArrayCollections so you might as well go straight for dictionary.


Another possibly better way is use a dictonary to hold the instances by key name.

var _htArrayMap:Dictionary = new Dictionary();
_htArrayMap["ary1"] = [1,2,3,4];

you can use a string variable as the key.

Dictionaries in as3 are incredibly powerful, and efficent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜