ColdFusion's Auto-Struct-Generation Caveat

Posted October 4, 2006 at 1:40 PM by Ben Nadel

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.



Reader Comments

There are no comments posted for this web log entry.

Post A Comment

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.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 21, 2013 at 7:46 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
No luck. At least I have uncovered the cause, URLScan 3.1. Here is what I see in the IIS log when a file is over 30mb. 2013-05-21 23:29:05 10.105.45.128 GET /plupload/assets/jquery/jquery-1.8. ... read »
May 21, 2013 at 6:12 PM
Using Plupload For Drag & Drop File Uploads In ColdFusion
Ben, I did not see you after Pete Freitag's Lockdown session at cfObjective but he said that IIS sets file size limits at 30MB by default which just happened to be the threshold for file size when ... read »
May 21, 2013 at 11:51 AM
Ask Ben: Parsing Very Large XML Documents In ColdFusion
Looking at my first ever XML document that I have to parse and put into MS SQL 2000 with CF8. I get it to list the desired Field name, many times over, and have a long list of this field name displa ... read »
May 21, 2013 at 9:25 AM
Turning Off and On Identity Column in SQL Server
you are awesome..i am lucky to get this blog between such a garbage one....Thanks, Prashant ... read »
May 20, 2013 at 4:38 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, Your confusion is well founded, since this is a very confusing features. In fact, it ONLY works if you use array notation. Meaning, that this: arrayToList( query[ "columnName" ] ) ... read »
May 20, 2013 at 4:34 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I was thinking chicken and the egg, I wouldn't have expected it to work in the valuelist going in I guess. Maybe I just need a beer, long day :) ... read »
May 20, 2013 at 4:29 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
@Dana, That's if you're trying to reference a specific row. In this case, we're trying to reference the entire query column as one cohesive value. So, you are correct that if you wanted to output a ... read »
May 20, 2013 at 4:24 PM
Using A Dynamic Column Name With ValueList() In ColdFusion
I thought when you used array notation to reference queries you always had to have the row or it would throw a similar error as well? ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools