Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at the New York ColdFusion User Group (Feb. 2009) with: Clark Valberg and Joakim Marner
Ben Nadel at the New York ColdFusion User Group (Feb. 2009) with: Clark Valberg@clarkvalberg ) and Joakim Marner

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.

Looking For A New Job?

100% of job board revenue is donated to Kiva. Loans that change livesFind out more »

Reader Comments