<cfif structKeyExists(messageCFC, "messageid")>
presuming messageCFC is an instance of message ?
Paul
Take this simple example, there is clearly a bug in some of these CF functions
<cfset test(1, 2) />
<cffunction name="test">
<cfargument name="a1" />
<cfargument name="a2" />
<cfargument name="a3" />
<cfargument name="a4" />
<cfoutput>#structCount(arguments)#</cfoutput>
<cfdump var="#arguments#" />
<cfoutput>#structKeyExists(arguments, 'a3')#</cfoutput>
</cffunction>
OUTPUTS
4
struct | |
A1 | 1 |
A2 | 2 |
A3 | undefined |
A4 | undefined |
NO
So
structCount counts it
<cfdump can display it
structKeyExists says it doesn’t exist
So it kind of half exists.
Regards
Dale Fraser
--
You received this message because you are subscribed to the Google Groups "cfaussie" group.
To post to this group, send email to cfau...@googlegroups.com.
To unsubscribe from this group, send email to cfaussie+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/cfaussie?hl=en.
This however works
<cfset test(1, 2) />
<cffunction name="test">
<cfoutput>#structCount(arguments)#</cfoutput>
<cfdump var="#arguments#" />
<cfoutput>#structKeyExists(arguments, 'a3')#</cfoutput>
</cffunction>
OUTPUTS
2
struct | |
1 | 1 |
2 | 2 |
NO
So it seems that when you use <cfargument your semi creating a structure of arguments that kind of half exist.
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: cfau...@googlegroups.com [mailto:cfau...@googlegroups.com] On
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
Cheers
Kai
--
Kai Koenig - Ventego Creative Ltd
ph: +64 4 476 6781 - mob: +64 21 928 365 / +61 435 263 414
web: http://www.ventego-creative.co.nz
blog: http://www.bloginblack.de
twitter: http://www.twitter.com/agentK
--
But the key to the struct exists J I can dump it.
Well I originally said no because
Accessing it other ways fails
But it’s clearly there, it needs to be consistant
The name of structKeyExists is perhaps wrong, because it really releates to the data, not the key.
I’ll repeat, in case the point was missed: if the concern here is that structkeyexists doesn’t do the job, note again that the arguments struct can be treated as an array as well as a struct. And the arrayisdefined function (new since 8) can test for the existence (or not) of an element in the array—and there will be no element in the array for an argument which was defined with cfargument but not passed in on the call (which was the concern originally presented). Hope that helps someone.
/charlie
From: cfau...@googlegroups.com [mailto:cfau...@googlegroups.com] On Behalf Of Dennis Clark
Sent: Friday, January 06, 2012 2:51 AM
To: cfau...@googlegroups.com
Subject: Re: [cfaussie] Count the number of arguments passed into a function.
Unfortunately it's too late to strive for consistency at this point.
The odd behaviour of structKeyExists is due to the underlying Java implementation, the inconsistent handling of null values in Adobe CFML, and backwards compatibility concerns.
CF structs are special Java objects that implement the java.util.Map interface. The method to retrieve a value in a Map is get(key). Map.get(key) returns a Java null if the key does not exist in the map. However if the <snip>
<cffunction name="blah">
<cfargument name="messageid" required="false" />
<cfargument name="text" required="false" />
<cfargument name="queryid" required="false" />
<cfargument name="datasource" required="false" />
<cfset var local = {} >
<cfset local.mystruct.1 = arrayIsDefined(arguments, 1)>
<cfset local.mystruct.2 = arrayIsDefined(arguments, 2)>
<cfset local.mystruct.3 = arrayIsDefined(arguments, 3)>
<cfset local.mystruct.4 = arrayIsDefined(arguments, 4)>
<cfreturn local.mystruct>
</cffunction>
Gives the following;
struct | |
---|---|
1 | NO |
2 | YES |
3 | YES |
4 | NO |
Which will solve the issue I am having, thanks Charlie.
Though I still think the implementation should be consistent across all functions.
Gavin.
Glad to help. Thanks for the update.
/charlie
From: cfau...@googlegroups.com [mailto:cfau...@googlegroups.com] On Behalf Of Gavin Baumanis
Sent: Wednesday, January 11, 2012 12:14 AM
To: cfau...@googlegroups.com
Subject: Re: [cfaussie] Count the number of arguments passed into a function.
Hi All,
Just to follow up on this - now that I am back to working on this issue...
And, not that I ever doubted Charlie... but a some working code is worth a thousand words...
<cffunction name="blah">
<cfargument name="messageid" required="false" />
<cfargument name="text" required="false" />
<snip>