I have been trying to figure out how to send a variable, logic-based selection of arguments as unnamed values to a function. My original thought was to use ColdFusion's CFInvoke and CFInvokeArgument, but CFInvokeArgument requires a name attribute (which is what I was trying to avoid). I couldn't just call the function using function notation (ex. "Test()") as I needed to involve logic in my argument selection (which only CFInvoke would allow).
Sean Corfield had a good idea to use the index-like keys. But for some reason, it didn't click in my head until Christoph Schmitz suggested the same thing. While both suggested this solution, I think it only dawned on me "how" to accomplish this when Chris said it (right place, right time). Both are clearly rock stars though.
So anyway, here is my test to see that it works. These are two functions that I tested with. The first, DumpArgs() is the ColdFusion UDF that just dumps out its arguments. The second function, StructCreate() is just a utility function (seen all over the place) that builds a struct on the fly and returns it:
Launch code in new window » Download code as text file »
Ok, now I need to create some values that I may OR may not need to pass in. It's friday, let's build a struct of hot girls that we can choose from:
Launch code in new window » Download code as text file »
Now that we have a our function in place and our possible values, let's create a variable-length argument collection based on some business logic:
Launch code in new window » Download code as text file »
This gives us the following CFDump:
| | | | ||
| | ![]() | | ||
| | | |
That works perfectly! I think the thing that finally clicked in my head when Chris suggested the argumentCollection was how to build the struct without having to use an incrementing value (ie. keeping some sort of argument count variable). By using the StructCount() I was able to just keep appending values to the struct and not having to worry about what index I was actually working on. Sweet!
Note: Using argumentCollection can also be done with standard function invokation. It does not require the use of CFInvoke.
Download Code Snippet ZIP File
Comments (4) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Nylon Technology Presentation: ColdFusion Query Object Primer
Lenny And Bo, ColdFusion Programmers (Vol. 3)
Ben, take a look at my experimental CFC framework. This is exactly how i pass data between different methods. There are a few different viewlets demonstrating how it all works.
http://labs.webapper.net/projects/CFCFramework/index.cfm
Posted by Steve Nelson on Mar 30, 2007 at 11:13 AM
FWIW, #dumpargs(argumentcollection=objArgs)# would work just as well as <cfinvoke method="DumpArgs" argumentcollection="#objArgs#" />
Posted by Matt Osbun on Mar 30, 2007 at 12:51 PM
@Steve,
The PPT looks cool. I will take try to take a look at the other files this weekend.
@Matt,
Yeah, good point. I was just using CFInvoke cause that's how I started out. But you are absolutely right.
Posted by Ben Nadel on Mar 30, 2007 at 2:48 PM
@Steve,
I finally got around to going through your CFC framework. It looks pretty interesting. I see what you are saying about passing around all of your arguments like this; you are continually passing your ARGUMENTS scope onto other methods.
I don't know enough MVC / Fusebox to comment on the functionality, but it does look very clean. I am sure it will positively influence my future thought processes.
Posted by Ben Nadel on Apr 4, 2007 at 6:31 PM