Steven Erat's Blog
 
 
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

About This Site

 
I live west of Boston and work as a Software Engineer with ColdFusion and Flex, specializing in Linux. Recently I graduated in Professional Digital Photography from CDIA.
More about me

Recent Entries

 
Recent Tweets for Wed July 01..
I'm Twittering This! or Dude..

Recent Comments

 
Posted By Marie Payne:
I really enjoyed those pictures you took in Alaska. Thank you so much for sharing, for I've been ill and had major surgery. I can't get out and do t ...

Posted By Julia:
First time that I see such photos. Too bad that I don't have Photoshop. I have to ask somebody to do them for me.

Posted By Steven Erat:
See also this collection of images from Dewey Mclean of his experience in the Army in the Korean War [link] His photographs and maps are very we ...

recently played

 
Zombie Nation
by Future Shock
on 90's Club Hits Reloaded
90's Club Hits Reloaded, Future Shock

now playing, a plug-in for itunes

Categories

 
RSS Adobe (31)
RSS Bicycling (9)
RSS Blogging (38)
RSS Books (13)
RSS Breeze (12)
RSS CFMX Podcasts (10)
RSS ColdFusion (420)
RSS Computer Technology (51)
RSS Events (25)
RSS Flash (3)
RSS Flex (20)
RSS Gadgets (10)
RSS HiTech Industry (16)
RSS Java (25)
RSS Learning (57)
RSS Linux (70)
RSS Mac OS X (22)
RSS Macromedia (27)
RSS Meetup (34)
RSS New England (62)
RSS Odds & Ends (25)
RSS Outdoors (32)
RSS Personal (29)
RSS Photography (110)
RSS Photoshop (29)
RSS Podcasts (18)
RSS Rants (19)
RSS Restaurants (8)
RSS Science (34)
RSS Spain (16)
RSS Travel (42)
RSS Twitter (2)
RSS Video (20)
RSS Webcam (3)
RSS Writing (10)

Blogs I Read

 
Terrence Ryan
Ben Forta
Ray Camden
Kinky Solutions
Dan Vega
Gary Gilbert
Simeon Bateman
Red Hat Blogs
O'Reilly Digital Media
O'Reilly Radar
John Nack
The Strobist
Scott Kelby
Matt Kloskowski
Joe McNally
Digital Photography School
Engadget
Science Blog

RSS

 


Add to Google
Add to My Yahoo!

Aggregated By

 


Consumed By Feed-Squirrel.com
Aggregated by ColdFusionBlogger.org

Credits and Stuff

 
BlogCFC - Free ColdFusion Powered Blog Software
CJM Group - ColdFusion Website Hosting


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