structKeyExists

Description

Determines whether a specific key is present in a structure.

Categories

Syntax

StructKeyExists(structure, "key")

Attributes

AttributeDescriptionRequiredDefault
keyKey to test
structureName of structure to test

Returns

True, if key is in structure; if structure does not exist, ColdFusion throws an exception.

Usage

This function can sometimes be used in place of the IsDefined function, when working with the URL and Form scopes, which are structures. The following pieces of code are equivalent:
cfif IsDefined("Form.JediMaster")> <cfif StructKeyExists(Form,"JediMaster")> A structure’s keys are unordered.

Example

<!--- This example shows the use of StructKeyExists. ---> 
This file is similar to addemployee.cfm, which is called by StructNew, 
    StructClear, and StructDelete. To test, copy the &LT;CFELSEif&GT; 
    statement to the appropriate place in addemployee.cfm. It is a custom tag 
    to add employees. Employee information is passed through the employee 
    structure (the EMPINFO attribute). In UNIX, you must also add the Emp_ID. 
 
<cfswitch expression = "#ThisTag.ExecutionMode#"> 
<cfcase value = "start"> 
    <cfif StructIsEmpty(attributes.EMPINFO)> 
<cfoutput>Error. No employee data was passed.</cfoutput> 
    <cfexit method = "ExitTag"> 
    <cfelseIf NOT StructKeyExists(attributes.EMPINFO, "department")> 
<cfscript>StructUpdate(attributes.EMPINFO, "department", 
"Unassigned"); 
        </cfscript> 
<cfexit method = "ExitTag"> 
         <cfelse>