I think it's a bug one way or another and Adobe should fix it.

structCount is INCORRECT imo shows 4 elements
dump is INCORRECT imo shows 4 elements
structKeyExists is CORRECT. It doesn't think its there.

Anything else you try to do to those elements, think it doesn't exists thus
the first 2 don't follow the same rules

<cfoutput>#arguments.a4#</cfoutput>

Element A4 is undefined in ARGUMENTS.

One way or another the behaviour should be consistant.

Regards
Dale Fraser

http://dale.fraser.id.au
http://cfmldocs.com
http://learncf.com
http://flexcf.com


-----Original Message-----
From: cfaussie@googlegroups.com [mailto:cfaussie@googlegroups.com] On Behalf
Of charlie arehart
Sent: Thursday, 5 January 2012 4:55 PM
To: cfaussie@googlegroups.com
Subject: RE: [cfaussie] Count the number of arguments passed into a
function.

Yep, that's an interesting challenge, Gavin. (And if anyone may have read
only the first sentence of his note and not the rest, note that he IS
already counting the arguments struct, but the issue is that because he's
using cfargument to define them, even without any default, all those defined
args appear in the struct even if no value is passed in.)

Ben Nadel has blogged on this same problem a few years ago:

http://www.bennadel.com/blog/1430-ColdFusion-ARGUMENTS-Keys-Always-Exist-Eve
n-When-Not-Required.htm

Sadly neither he nor anyone else reading it had any solution other than what
you've proposed, Gavin, doing a structkeyexist to test if there's a value
passed in.

I do agree that it could be helpful to get the value more easily, and I'm
really surprised that it's not something that's been solved (though I have
to admit I've not needed it before, but I can appreciate its value.) Perhaps
someone here will point out something we're all missing. :-)

One little tip that could help find another solution is that the arguments
scope can be processed either as a struct or an array. While
arraylen(arguments) returns the same value as structcount, but the slot in
the array for any argument not provided will be empty, and that can be
tested as of CF8 with arrayisdefined(array,index). Not really any better
than structkeyexists. Again, though, maybe thinking about it as an array
instead may give someone an idea.

Finally, I'll note that I searched the cflib.org site, and of the 55 UDFs
there that refer to "count", none seemed related to solving this problem.
Same with the 21 that referred to "arguments".

/charlie


> -----Original Message-----
> From: cfaussie@googlegroups.com [mailto:cfaussie@googlegroups.com] On 
> Behalf Of Gavin Baumanis
> Sent: Wednesday, January 04, 2012 11:55 PM
> To: cfaussie
> Subject: [cfaussie] Count the number of arguments passed into a 
> function.
> 
> Hi there,
> Let me explain what I am trying to do,
> 
> the executive summary is that I want to count the number of arguments 
> passed into a function.
> anyFunction(), might have 10 cfargument properties assigned to it.
> But if I call;
> anyFunction(arg1="1", arg2="2")
> 
> Then I somehow want to be able to retrieve the value 2 (2 passed in 
> variables).
> 
> Why,
> Because I have a getObject method.
> getObject is basically a cfquery and I filter the results of the 
> query, based on the arguments passed in.
> so if I have;
> getObject(myId = 1, yourId = 2);
> 
> then it creates SQL that looks like;
> select STUFF
> from myTable
> where 1=1
> and myid=1
> and yourid=1
> and...(add a number of arguments here....)
> 
> If I do NOT pass in any arguments, then the SQL retrieves ALL rows 
> from the table.
> But I want to do is;
> If no arguments are passed in  - create an empty object.
> 
> (we have a getAllObjects function to retrieve all rows)
> 
> maybe some code will help...
> 
> I have two snippets of code;
> One is a function in a CFC, the other is some code in a CFM template 
> that calls the function - pretty simple.
> 
> myTestCFC.cfc:
> <cffunction name="blah">
>       <cfargument name="text" required="false" />
>       <cfargument name="messageid" required="false" />
>       <cfargument name="queryid" required="false" />
>       <cfargument name="datasource" required="false" />
> 
>       <cfset var local = {} />
>       <cfset local.length = #structCount(arguments)#>
> 
>       <cfreturn local.length>
> </cffunction>
> 
> 
> test.cfm:
> <cfset myReturnVal = myTestCFC.blah(text="gav", queryid=3)> <cfdump 
> var="#myReturnVal#"> <br /> <cfif structKeyExists(messageCFC, 
> "messageid")>
>       Yes
> <cfelse>
>       No
> </cfif>
> 
> the output of this code is;
> 4
> No
> 
> The value 4 is because blah() has 4 arguments defined.
> But structKeyExists, returns false...
> 
> How can it not exist but be accounted for?
> surely one of the values is wrong?
> 
> If I change the code slightly to be;
> <cfset local.length = #structKeyList(arguments)#> instead of; <cfset 
> local.length = #structCount(arguments)#>
> 
> Then all four arguments defined in the "stub" of the function are 
> listed.
> 
> So is one of the values being returned a bug?
> 
> and lastly,
> How do I go about "counting" actual passed in aerguments?
> (I could write another function that loops through 
> structKeyLists(arguments), then do a structKeyExists[i] and increment 
> a counter and do my own accounting....
> but it just "seems" like there should be a nicer way to do it...
> 
> As always - Thanks for any thoughts!
> 
> Gavin.
> 
> --
> You received this message because you are subscribed to the Google 
> Groups "cfaussie" group.
> To post to this group, send email to cfaussie@googlegroups.com.
> To unsubscribe from this group, send email to
> cfaussie+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/cfaussie?hl=en.


--
You received this message because you are subscribed to the Google Groups
"cfaussie" group.
To post to this group, send email to cfaussie@googlegroups.com.
To unsubscribe from this group, send email to
cfaussie+unsubscr...@googlegroups.com.
For more options, visit this group at
http://groups.google.com/group/cfaussie?hl=en.


-- 
You received this message because you are subscribed to the Google Groups 
"cfaussie" group.
To post to this group, send email to cfaussie@googlegroups.com.
To unsubscribe from this group, send email to 
cfaussie+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/cfaussie?hl=en.

Reply via email to