Define unique variable name

ⅰ亾dé卋堺 提交于 2019-12-12 02:46:36

问题


How to create a dynamic ArrayCollecton instance that use unque naming: ac1, ac2..ac999 whether user click a button. Without having to use hardcode variable name.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/6001518/define-unique-variable-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!