3

I need to pass a structure to a method, but it will not always be defined.

Is there something like this that would work?

<cfparam name="system_message" default={}>

When I try this I get, the argument passed to the function is not of type struct.

Also, I realize, I could do this:

<cfif ! isdefined("system_message")>
      <cfset system_message = {}>
</cfif>

But I was just wondering if there was a shorter way of doing it, using cfparam.

Thanks for any help!

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
RandyLahey
  • 979
  • 4
  • 10
  • 26

3 Answers3

9

What about:

<cfparam name="system_message" default="#StructNew()#">

CF8 doesn't like the curly braces version.

RobG
  • 1,751
  • 2
  • 11
  • 7
3

You're close. You'll need to write it as:

<cfparam name="system_message" default="#{}#">
Peruz Carlsen
  • 572
  • 2
  • 10
  • When I use that, I am getting this error: Invalid CFML construct found on line 5 at column 50. ColdFusion was looking at the following text: {. Coldfusion 8 by the way, not sure if that matters here. – RandyLahey Jan 27 '12 at 16:33
  • 1
    CF 8 does matter. As @RobG noted above, CF 8 does not support that notation for creating structs. Use StructNew() instead. – Sean Coyne Jan 27 '12 at 16:53
  • Since @RobG's answer is more correct (because of the version of ColdFusion being used) you should accept his answer instead. – ale Jan 27 '12 at 18:51
3

If you are passing this to a method, you should consider using <cfargument> within a <cffunction> call rather than the more global <cfparam>. The same "default" attribute applies. Then you know your variable exists only within the ARGUMENT scope within the function, better encapsulation!

<cfargument name="system_message" default="#structNew()#">
ale
  • 6,369
  • 7
  • 55
  • 65
Dan A.
  • 2,914
  • 18
  • 23