Reputation: 10932
Adobe specifically states about Dictionaries:
the object's identity is used to look up the object, and not the value returned from calling toString()
However when I run
if(myInstance in myDictionary) { ... }
To see if myInstance already exists as a 'key' in myDictionary, myInstance.tostring get's called!
Anyone know why or a way around it?
Upvotes: 0
Views: 165
Reputation: 18193
The in
keyword is generally used with object properties, which are strings. Consider the difference between a for each
loop versus a for in
loop.
We typically use the for in
loop to iterate over an object's dynamic properties:
private var o:Object = { property1: "value1", property2: "value2" };
for (var propertyName:String in o)
{
trace(propertyName);
trace(o[propertyName]);
}
Outputs:
property1
value1
property2
value2
So in your code snippet the in
keyword is causing the call to toString()
.
The correct way to test if a key exists is to test for null
:
if (myDictionary[myInstance])
trace("key exists and it has a value");
Upvotes: 1