What you need is similar to the Dictionary
class in .NET, and it is available by default in the Object
AS3 class.
var key:String="hello"; var value:int=100; var map:Object=new Object(); map[key]=value;
Now you can get the saved value as
trace(map[key]);
If you want, you can also build on it to make the class as follows:
public class Dictionary { private var _keys:Array; private var _maps:Object; public function Dictionary() { _keys=[]; _maps:Object={}; }
And you can use it like:
var dict:Dictionary=new Dictionary(); //set the value dict.setProperty("flash", 10); //get the value dict.getProperty("flash"); //delete if not required dict.deleteProperty("flash");
Note that this can be used to set keys and values โโof any type. It also solves the ReferenceError
problem if the property is not defined and you do not need to use any dynamic class in YOUR code (since the Dictionary
class processes it internally).
One of the most common ways to use this would be to define parsing functions. for example in a spreadsheet viewer:
var parseFuncts:Dictionary=new Dictionary(); parseFuncts.setProperty("csv", Parsers.parseCSV); parseFuncts.setProperty("xml", Parsers.parseXML); parseFuncts.setProperty("txt", Parsers.parseTXT); parseFuncts.setProperty("xls", Parsers.parseXLS); parseFuncts.setProperty("xlsx", Parsers.parseXLSX); var fileToOpen:File;
source share