CFFunction's CFArgument can't takes NULL as a value when the parameter's data type is numeric

1,645 views
Skip to first unread message

prashan...@gmail.com

unread,
Jan 27, 2009, 3:41:41 PM1/27/09
to CFCDev
Hi to all,

Correct me, if I am wrong or not using CFArgument in a right way.

I am unable to pass NULL as a value to CFArgument of data type
Numeric. Its giving me "not a numeric value" error.

Like we have NULL=yes in CFQueryParam, do we have same thing in
CFArgument?

-Prashant Roy

Charlie Griefer

unread,
Jan 27, 2009, 3:45:24 PM1/27/09
to cfc...@googlegroups.com
NULL works in cfqueryparam because the database understands the concept of NULL.  CF does not.  The best you can do in CF is to pass an empty string as the argument.  If you don't pass the argument at all, and don't give it a default value, you can also do a <cfif not structKeyExists(arguments, 'myArgumentName') />.
--
I have failed as much as I have succeeded. But I love my life. I love my wife. And I wish you my kind of success.

prashant roy

unread,
Jan 27, 2009, 4:29:12 PM1/27/09
to cfc...@googlegroups.com
thanks. agreed.
 
For optional numeric CFArguments parameters, we are not passing anything but still its giving error for data type. I m unable to understand this. Need your suggestions again.

Matt Quackenbush

unread,
Jan 27, 2009, 4:51:27 PM1/27/09
to cfc...@googlegroups.com
If you pass an empty string to a type="numeric" argument, an exception will be thrown because you're not passing a numeric value.  So, either don't pass an empty string and check for the existence of the argument inside the function, or pass a 0.

Jared Rypka-Hauer

unread,
Jan 27, 2009, 5:50:22 PM1/27/09
to cfc...@googlegroups.com
Yeah, you need to deal with the fact that "" is non-numeric.There's
actually a bunch of different ways to do this though... have your
cfargument with type="numeric" also have default="0", or pass in a 0
for that argument, or have type="any" or type="string" and do
isNumeric(arguments.myNumber) before doing anything with it. Check for
structKeyExists(arguments,"myNumber) before doing anything in your
function.

There's a bazillion ways to make sure the argument is correct... just
pick one and go with it.

J

Sammy Larbi

unread,
Jan 27, 2009, 6:02:15 PM1/27/09
to cfc...@googlegroups.com
I would venture away from passing in 0 to mean null. Unless you're sure there's no way it should ever be 0. And even then maybe not.

If you're providing types to your arguments mainly for documentation purposes, consider putting that it should be a number in the argument's hint attribute if the name is not obvious enough.

Sam

John Whish

unread,
Jan 28, 2009, 4:21:33 AM1/28/09
to cfc...@googlegroups.com
I'd suggest that you have: 
<cfargument name="foo" type="numeric" required="false" /> 
and then (as Charlie suggested above) do:
<cfif NOT StructKeyExists( arguments, "foo" )>
  <!--- if not passed, treat as a null value --->
<cfelse>
  <!--- argument passed --->
</cfif>

Alan Livie

unread,
Jan 28, 2009, 4:32:00 AM1/28/09
to cfc...@googlegroups.com
The <cfargument name="foo" type="numeric" required="false" /> means your calling code needs some conditionals too so it doesn't pass an empty string.

I'm all for

<cfargument name="foo" type="any" required="false" />

I used to have so much pain with generated beans and DAO's with numeric argument types I decided to just make them all type="any". I mean seeing a field countryId makes it fairly clear it will usually be a numeric so I don't care about explicitly specifying type.

My object args are usually type="Any" too.


Alan



From: John Whish <john....@googlemail.com>
To: cfc...@googlegroups.com
Sent: Wednesday, January 28, 2009 9:21:33 AM
Subject: [CFCDEV] Re: CFFunction's CFArgument can't takes NULL as a value when the parameter's data type is numeric

John Whish

unread,
Jan 28, 2009, 4:42:35 AM1/28/09
to cfc...@googlegroups.com
Hi Alan, pros and cons to both approaches. I quite like having a nice API when working with other developers (hence type="numeric"), but when in RAD mode then type="any" is the way to go. On the live box type checking should be disabled anyway so by specifying the type I know that dev code should be solid. 
Reply all
Reply to author
Forward
0 new messages