CF TIP: Determing if a UDF argument is "null"

Posted by Dan on Sep 20, 2006 @ 10:58 AM

Ok, so the title is misleading. CF doesn't really have a concept of null, but it does have the concept of "not exists."

If you need to differentiate between when an argument of a function has been supplied or not, set the required attribute to true and leave the default attribute off. If the argument is not supplied to the function, then a against structKeyExists(arguments, "argumentName") will return false. If a value has been supplied, the function will return true.

Here's an example:

<cffunction name="isArgExists" returntype="boolean" output="true">
    <cfargument name="arg" required="false" />
    <cfreturn structKeyExists(arguments, "arg") />

    isArgExists: #isArgExists()#<br />
    isArgExists: #isArgExists("test")#

The output would be as follows:

isArgExists: NO
isArgExists: YES

There are times when using the default attribute to just supply a value is fine, but there are many times when you need to differentiate between when an argument was actually supplied or when it was not.

Categories: HTML/ColdFusion, Java, Source Code

Comments for this entry have been disabled.