ColdFusion Dynamic Argument Naming in Method Calls

I created a custom tag recently that required a method argument name be passed as an attribute. While I’m familiar with the usual techniques of using dynamic variable names, they didn’t work in this situation.

Here’s what I was attempting to do:

Main Page


<cfmodule template="customtags/deleteRecord.cfm"
				idValue = "1"
				idColumn = "colName" />

Custom Tag


<cfset obj = createObject("component","com.mycomponent")>
<cfset qRecord = obj.getRecord(attributes.idColumn = attributes.idValue) />

The idValue wasn’t an issue but the idColumn wouldn’t work. I tried Evaluate and “#attributes.idColumn#” and a few other similar methods to no avail. Finally, I remembered that you can pass a structure as an argument collection. So I ended up creating a new structure and passing it like this:


<cfset methodParms = structnew() />
<cfset methodParms[attributes.idColumn] = attributes.idValue />
<cfset qRecord = obj.getRecord(argumentcollection = methodParms) />

And that finally worked.

If there’s another solution out there, please enlighten me.

-rG

coldFusion, quickTip

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

7 Responses to “ColdFusion Dynamic Argument Naming in Method Calls”

Leave Comment

(required)

(required)