Enlisted 03/09/2013 to Archive[as3, dictionary, null, undefined, dynamic class]

AS3: How to check if a value of a given key exists in Dictionary?

Most of AS3 rookies encounter a problem, when they need to check whether a given value exists in the Dictionary And most of them, according to some general principles, are forced to do it wrong :) Well, lets’s see how to do it properly.

Dictionary vs. Object

The Dictionary is very similar to the Object class, but Dictionary unlike of the Object class can use Objects as keys and has constructor with parameter determining the weakKeys property. Now, lets's see the difference between accessing properties of dynamic Object and Dictionary object:

	// Object class
	var obj:Object = {};
	obj.key1 = 5; // Access like property
	obj.key2 = "five";

	// Dictionary class
	var dict:Dictionary = new Dictionary();
	dict["key1"] = 5; // Acces like array field
	dict["key2"] = "five";
	dict[obj] = "obj"; // Using Object as a key

It is obvious that way of handling these classes is very simillar. Dictionary has this special ability (among the many disabilities :D) that it behaves as a dynamic class but allows mapping of non-String keys. The second significant feature is iterability. You can iterate over the keys and access mapped values, while Object class lacks this ability. Let's focus now on the dealbreaker.

Check if given value exists in the Dictionary

First of all be sure of understanding the difference between null and undefined objects. Common bad usage is:

	if (dict["key"] != null)
		// do code when value does exist
		// do anything else

The problem is, that value stored in dictionary could be null, therefore the value mapped to a given key actually does exist, but it's null. There is an internal type conversion applied when this condition is used. It is not hard to think out to what is the null value converted, it is the undefined object. Compare these cases on an empty Dictionary:

	var dict:Dictionary = new Dictionary();
Table 1: Comparison of check conditions
Condition Result Reason
dict["key"] == null
Due to the internal type conversion of null to undefined and there is no defined value for the given key.
dict["key"] === null
Thanks to strictly typed comparison no type conversion takes place now and because no null value is mapped with the given key, false is returned.
dict["key"] == undefined
These comparisons are equal and because there is no defined value for the given key, true is returned.
dict["key"] === undefined

The special case and often mistake also occurs when simplified construction

	if (dict["key"])
		// do code when value does exist

is used. There is a huge risk that boolean false or zero value is returned and the condition would be badly evaluated! Use this way only if you are really sure these values cannot be returned. My recommendation is to avoid this condition completely.


So here we are, at the end. Let's summarize it a bit. We understand now how the value checking works on Dictionary object. The best and safest ways is

	if (dict["key"] !== undefined)
		// do code when value does exist

and there is really no difference in using strictly typed comparison or not. Just remember the difference between null and undefined.



Please feel free to leave a comment related to the topic. Posts violating the netiquette will be deleted.

comments powered by Disqus