Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
I am the chief technical officer at InVision App, Inc - a prototyping and collaboration platform for designers, built by designers. I also rock out in JavaScript and ColdFusion 24x7.
Meanwhile on Twitter
Loading latest tweet...
Ben Nadel at the New York ColdFusion User Group (Sep. 2008) with:

ColdFusion's Auto-Struct-Generation Caveat

By Ben Nadel on
Tags: ColdFusion

One thing that ColdFusion will do for you is auto-generate struct objects when they are referenced as part of a variable name. For instance, if you have no previously defined variables and you run this line of code:

  • <!--- Set key value. --->
  • <cfset objCouple.Boy.Name = "Ben" />

... ColdFusion will automatically create the structure "objCouple". Then, within that structure, it will automatically create the key, "Boy", and set it equal to a new struct. Then it will automatically create the key, "Name", within that structure and assign the value "Ben" to that key.

I personally never do this as I like to explicitly define my structures, but, it's kind of neat. The caveat though, is that this only works with "valid variable names". I am not sure what the exact definition of that is, but basically you can only have names that begin with letters and under scores (I think), but the big caveat is that you cannot use array notation when defining this struct. For example:

  • <!--- Set key value. --->
  • <cfset objCouple[ "Girl" ].Name = "Molly" />

... throws a coldfusion.runtime.UndefinedElementException:

Element Girl is undefined in a CFML structure referenced as part of an expression.

So, I personally wouldn't recommend letting ColdFusion auto-generate anything for you (using this methodology or through the use of StructGet() for that matter), but if you are going to, just be careful about how you want to name things.

Tweet This Titillating read by @BenNadel - ColdFusion's Auto-Struct-Generation Caveat Thanks my man — you rock the party that rocks the body!


Reader Comments

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.