Pretty banner! :)

Another Common ActionScript Mistake

1s & 0s This isn’t The Most Common Stupid Programmer Mistake, but it’s annoying enough to have it specifically mentioned.

This particular guy crops up when we’re dealing with dictionaries. Dictionaries are infinitely useful (though they have some gotchas). This is probably one of their most important gotchas.

Setting an Entry To Null

dictionary[ key ] = null;

Deleting an Entry

delete dictionary[ key ];

These do not accomplish the same task, though they may seem to. Setting it to null will not remove the entry from the dictionary, it will merely set its value to null. 99% of the time this is not what you want. You’ll only realize it has not done what you wanted when it comes time to iterate over the dictionary with a loop. The null key will still show up as an entry in the dictionary during this iteration, where as a deleted key does not.

Just remember that you almost always want to delete dictionary entries, rather than simply set them to null.

Post Metadata

Date
September 4th, 2008

Author
urbansquall

Category

Tags


1 Comments


  1. NickZA

    Weirdest thing, I saw this in your article on the Serenity framework, was about to post on that article to ask why you’d used the delete syntax on the dictionary in your EntityContainer… and then found this completely by mistake. Cheers for posting.


Leave a Reply