View comments | RSS feed

StructKeyExists

Description

Determines whether a specific key is present in a structure.

Return value

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

Category

Decision functions, Structure functions

Syntax

StructKeyExists(structure, "key")

See also

Structure functions

Parameters

Parameter Description
structure
Name of structure to test
key
Key to test

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. --->
<p>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>

ColdFusion 9 | ColdFusion 8 | ColdFusion MX 7 | ColdFusion MX 6.1 | ColdFusion MX | Forums | Developer Center | Bug Reporting

Version 6

Comments are no longer accepted for ColdFusion MX. ColdFusion 8 is the current version.

Comments


alesha said on Nov 13, 2002 at 6:04 PM :
The documentation indicates that
isDefined(form.value)
is equivalent to
StructKeyExists(form, "value")

however, isDefined throws an error saying "Value is undefined in form."

and I'm thinking, well... duh, that's what I'm testing for. StructKeyExists() appears to do what ParameterExists() used to do.

How are these functions "equivalent" and why are they different?

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/coldfusion/6/CFML_Reference/functions-pt299.htm