Steven Erat's Blog Steven Erat Photography
 
 
Viewing By Entry
 
 

TalkingTree  How do CFCs see arguments passed by argumentcollection?

 

This was a question presented to me earlier today, and while it may be a yawn for many of you, I thought I'd review it here since it stumped me for a little while until I read the docs again.

The problem was that the calling page created a structure and populated a few struct keys with Strings, then passed the whole struct as input to a CFC method with <cfinvoke argumentcollection="#myStruct#" ...> when myStruct was a required input argument of type Struct, but the CFC returned the exception:

The parameter MYSTRUCT to function handleArgCollection is required but was not passed in.

The example presented to me was slightly deeper where CFC1 method1 was invoking CFC2 method1 and passing it the struct as an argumentcollection.

When listening to people explain problems its easy to supplant what you know about a subject with the suggestion of what is being described to you. The power of suggestion I suppose. So that is how I got sidetracked into building a reproducible case for this because for a while I forgot what I knew and assumed that what I was being shown was the right way to do it.

The problem was caused by identifying the Struct as a required input parameter of type Struct in method's cfargument tag and then referencing arguments.myStruct.keyname. The proper way to do this instead is to list the keynames as required arguments of type String (or whatever the keys contain), and then reference them by arguments.keyname.

To me, this issue exemplifies why I prefer to receive the shortest possible description of a problem with the smallest example code instead of listening to a lengthy description with extraneous code. Sometimes the power of suggestion just takes over your previous inclinations and pushes the solution that much further away. What I should have done first is to confirm what the docs say about how argumentcollections are handled:

When you pass an argumentCollection structure, each structure key is the name of a parameter inside the structure.

The example CFC invocation of Comp2 from the CFC Comp1 is:

<cfset var result = "">
<cfset var myStruct = structNew()>
<cfset myStruct["a"] = 1>
<cfset myStruct["b"] = 2>
<cfset myStruct["c"] = 3>

<cfinvoke      
argumentcollection="#myStruct#"
component="Comp2"
method="handleArgCollection"
returnvariable="result" />

The CFC method argument declaration and usage that produced the error is:

<cfargument name="myStruct" required="yes" type="struct">
      <cfset msg = arguments.myStruct["a"] & arguments.myStruct["b"] & arguments.myStruct["c"]>

And the corrected usage is:

<cfargument name="a" required="yes" type="string">
<cfargument name="b" required="yes" type="string">
<cfargument name="c" required="yes" type="string">
      <cfset msg = arguments["a"] & arguments["b"] & arguments["c"]>

 


Comments

Good post. While I knew this already, I see almost no one use this feature (or attributeCollection in custom tags), so I'm happy you are reminding people about it.


Hi Ray! I'm glad you post this article because I was trying to pass a structure to my cfc and got all sort of errors. I found an article from adobe site about using the argumentcollection attribute, this pointed me to why mi code did not work but without any example I wasn't able to make my code work. Your example is short but clear so thank you again!!! Your blog have always been my favorite!!! I even bought your books!


@BYJ: This is not my blog post. It is a blog post by Steven Erat. I just posted a comment.


Ray, If I understanding this correctly. First I'm passing a structure to my cfc via cfinvoke but then in my cfc since my arguments are all of string type then I lost my structure am I right? In another words, I can no longer use my structure inside my cfc anymore.
If I need to further process my argument as a structure I need to re-create a structure within my cfc using all the arguments strings?


You can pass a structure to a CFC. However, if you use "argumentCollection" in cfinvoke, it is special. When you do argumentCOllection=s, where S is a structure, CF acts as if each key of the structure S was a real argument. So consider a structure with 2 keys:

s.name = "Ray"
s.age = 35

If I do cfinvoke component="foo" method="something" argumentCollection="#s#"

it is the EXACT same as component="foo" method="something" name="Ray" age="35"

Basically, using argumentCollection is a way to dynamically pass arguments to a method. If you don't need that feature, then don't use it.

If you want to pass a struct to a CFC method as just a simple struct, nothing prevents you from doing that:

cfinvoke component="foo" method="goo" data="#s#"

Will give you an argument named Data in your method that has the key/name values from s.


 

 

Calendar

 
Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

Search This Site

 
This is an exact search only

topics

 
adobe blogging coldfusion computer technology events flex java learning linux mac os x macromedia meetup new england odds & ends outdoors personal photos photoshop science travel video

About This Site

 
Adobe Alumni & Community Professional. Expert in ColdFusion, Flex, LCDS, Photoshop, Lightroom. Linux RHCE. Follow Me!. For my photography check out Boston Portrait Photographer.

Speaker at CF.Objective(): Automated UI Testing with CFSelenium, MXUnit, ANT, and JenkinsCI

Adobe Community Professional (ACP)
Red Hat Linux Certified Engineer

Recent Entries

 
Automated System Testing for ..
Could not find ColdFusion com..
No April Fools: Selenium Ship..

Recent Comments

 
Posted By Steven Erat:
Jim, and anyone else that may attend, if you would like the full slide deck and my demo project files BEFORE the conference, please reply as a comment ...

Posted By Jim Priest:
Can't wait for this one!!

Posted By iPhone Repair:
It appears there are so many people have issue with their iPhone & iPod Touch screens dropped and cracked. It happened to me also when u haven't got a ...

recently played

 
Mr. Brightside
by The Killers
on Hot Fuss
Get Hot Fuss by The Killers on Amazon

now playing, a plug-in for itunes

Categories

 
RSS Adobe (34)
RSS Bicycling (9)
RSS Blogging (39)
RSS Books (13)
RSS Breeze (13)
RSS CFMX Podcasts (10)
RSS ColdFusion (437)
RSS Computer Technology (51)
RSS Events (26)
RSS Flex (20)
RSS Gadgets (11)
RSS HiTech Industry (16)
RSS Java (26)
RSS Learning (57)
RSS Linux (70)
RSS Mac OS X (23)
RSS Macromedia (27)
RSS Meetup (35)
RSS New England (62)
RSS Odds & Ends (25)
RSS Outdoors (32)
RSS Personal (29)
RSS Photos (111)
RSS Photoshop (29)
RSS Podcasts (18)
RSS Rants (19)
RSS Restaurants (8)
RSS Science (34)
RSS Spain (16)
RSS Travel (42)
RSS Twitter (10)
RSS Video (20)
RSS Webcam (3)
RSS Writing (10)

RSS

 


Add to Google
Add to My Yahoo!

Credits and Stuff

 
BlogCFC - Free ColdFusion Powered Blog Software


 
 
blog | photos | flickr | referers | webcam | stats | about | contact
 
Copyright © 2012 Steven Erat. All rights reserved.
This is a personal weblog. The opinions expressed here represent my own and not those of my employer