How can I define a Dictionary with string key in AS3? and how to do read operation?
for example:
var Dic:Dictionary = new Dictionary();
Dic["Exhausted"] = "He who talks more is sooner exhausted, please keep smiling :)";
String str = str.substring(8,str.length-1); // == str = "Exhausted";
trace('Dic[' + str + '] = ' + Dic[str]);
the output is Dic[Exhausted] = undefined???!!
why?
I think you have a syntax error on this line:
If you just use this :
You can see it’s all fine.
The problem with
var str:String = str.substring(8,str.length-1);is that you define a String named “str”:var str:String, but you assign a value which is the result of thesubstring()method called on str and of cours str does not exist yet when you call substring on it.Not sure if this makes sense: you define str as the result of applying substring on str.
The actionscript compiler should’ve complained btw:
Just a wild guess, but would you happen to have str defined somewhere else in your code and you just update str again using str before using it ? In which case, you shouldn’t redefine, str, you should simply assign a new value.
e.g.
Also, another syntax error is how you define
str:should be
var str:String(as2/as3 syntax), notString str(java/c++/etc. style)