AS3: using a string as a variable

Is it possible to extract the name of a variable from a string and use it as a variable

var myvar:String = "flash"; var flash:Number = 10; trace( myvar as variable ); 

something like that

+4
source share
3 answers

You can use it as a property of an object.

 public dynamic class MyClass { function MyClass(propName:String, propValue:*) { this[propName] = propValue; } } 

or

 var myvar:String = "flash"; var o : Object = {}; o[myvar] = 10; trace(o.flash); //10 

If you don't know what the property name will be, you should use a dynamic class ( Object is dynamic by default)

+4
source

A variable name in the form of strings can be performed as follows:

 this["myvar"] = "flash"; 

Notes:

  • This will raise a ReferenceError if the property has not been previously defined. And this refers to an object that is not dynamic .
  • You can, of course, replace this with the instance name of the object from which you want to use the property, for example. mySprite["x"] .
  • You can also use this method to call methods: this["addChild"](mySprite);
+5
source

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={}; } //Returns the array of all saved keys public function get keys():Array { return _keys; } //Sets a key-value pair public function setProperty(key:*, value:*):int { _maps[key]=value; return _keys.push(key); } //Returns the value stored at a particular key public function getProperty(key:*):* { if(_keys.indexOf(key) != -1) { return _maps[key]; } return null; } //Removes a key-value pair if it exists and returns if the key existed public function deleteProperty(key:*):Boolean { var ix:int; if((ix=_keys.indexOf(key)) != -1) { //dissociate value from key delete _maps[key]; //delete key _keys.splice[ix,1]; return true; } return false; } } 

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; //Already contains the file to open private function open():void { var f:Function=parseFuncts.getProperty(fileToOpen.extension); f.apply(this); } 
+1
source

Source: https://habr.com/ru/post/1413482/


All Articles