Tip when creating case sensitive structure keys in ColdFusion


Tip when creating case sensitive structure keys in ColdFusion

Posted by Luis Majano
Dec 16, 2008 00:00:00 UTC
This is a tip and reminder (I just spent like 10 minutes shouting why why why!!), of a very subtle difference when creating structures in CF. AT first glance creating something like the following will produce what you expect:

mappings = structnew();

mappings.FirstName = "Luis";

mappings.LastName = "Majano";

You would say that the keys for this structure would be "FirstName" and "LastName" would you? Well, the answer is NO!! The keys would be "FIRSTNAME" and "LASTNAME" because the are uppercased by CF. If you really want to keep case, then don't use dot notation but array notation:

mappings = structnew();

mappings["FirstName"] = "Luis";

mappings["LastName"] = "Majano";

By using array notation you are explicitly setting the key with the correct case. I re-discovered this gem by yes, UNIT TESTING, the ColdBox caching engine and some tests where failing because the keys could not be found. Well, DUUHHHH!!! You where assigning them wrong. Anyways, I thought I would share my stupidity on the matter and hopefully save someone or even myself in the future, the few minutes of "WHY WHY WHY!!"


Dutch Rapley

The same thing applies when doing Flash Remoting between ColdFusion and Flex. I've just gotten into the habit of always using the bracket notation.

Edward Smith

I discovered this (and also screamed, "WTF" when working on a CF wrapper around CouchDB.

John Farrar

Use this but be very careful if you are expecting ColdFusion to treat the structure as case sensitive.

mappings = structnew(); mappings["FirstName"] = "Luis"; mappings["LastName"] = "Awesome"; mappings["LastnAme"] = "Majano";

the value of mappings["LastName"] is now Majano even though the case doesn't match because of how ColdFusion works. (Sorry... in this case you would not be Awesome! lol)