View comments | RSS feed
Contents > CFML Reference > ColdFusion Tags > cfcase PreviousNext

cfcase

Used only inside the cfswitch tag body. Contains code to execute when the expression specified in the cfswitch tag has one or more specific values.

Flow-control tags

<cfcase 
value = "value or delimited set of values"
delimiters = "delimiter characters"
>

cfdefaultcase, cfswitch

Attribute

Req/Opt

Default

Description

value

Required

 

The value or values that the expression attribute of the cfswitch tag must match. To specify multiple matching values, separate the values with the delimiter character. The value or values must be simple constants or constant expressions, not variables.

delimiter

Optional

, (comma)

Specifies the delimiter character or characters that separate multiple values to match. If you specify multiple delimiter characters, you can use any of them to separate the values to be matched.

The contents of the cfcase tag body executes only if the expression attribute of the cfswitch tag evaluates to a value specified by the value attribute. The contents of the cfcase tag body can include HTML and text, and CFML tags, functions, variables, and expressions. You do not have to explicitly break out of the cfcase tag, as you do in some languages.

One cfcase tag can match multiple expression values. To do this, separate the matching values with the delimiter character, which is the comma by default. For example the following line matches "red", "blue", or "green":

<cfcase value="red,blue,green">

You can use the delimiter attribute to specify one or more delimiters to use in place of the comma. For example, the following line matches "cargo, live", "cargo, liquid", and "cargo, solid":

<cfcase value="cargo, live;cargo, liquid-cargo, solid" delimiters=";-">

<!--- The following example displays a grade based on a 1-10 score.
      Several of the cfcase tags match more than one score.
      For simplicity, the example sets the score to 7 --->
<cfset score="7">
<cfswitch expression="#score#">
   <cfcase value="10">
      <cfset grade="A">
   </cfcase>
   <cfcase value="9;8" delimiters=";">
      <cfset grade="B">
   </cfcase>
   <cfcase value="7;6" delimiters=";">
      <cfset grade="C">
   </cfcase>
   <cfcase value="5;4;" delimiters=";">
      <cfset grade="D">
   </cfcase>
   <cfdefaultcase>
      <cfset grade="F">
   </cfdefaultcase>
</cfswitch>
<cfoutput>
   Your grade is #grade#
</cfoutput>

Contents > CFML Reference > ColdFusion Tags > cfcase PreviousNext

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

Version 6.1

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

Comments


Steve said on Feb 17, 2004 at 8:23 AM :
<cfbreak> now also works in conjunction with a <cfswitch>. This is really screwy and isn't backwards compatible with cf5. Run this on a cf5 and a cfmx machine and you'll see what I mean:

<cfloop from="1" to="5" index="count">
<cfoutput>#count#<br></cfoutput>
<cfswitch expression="#count#">
<cfcase value="3"><cfbreak></cfcase>
</cfswitch>
<cfif count is 4><cfbreak></cfif>
</cfloop>

on cf5 it displays: 1 2 3

on cfmx it displays 1 2 3 4
halL said on Feb 18, 2004 at 1:19 PM :
This is a known issue in ColdFusion MX 6.1 (bug number 53339).
GAlanShepherd said on May 21, 2004 at 11:50 AM :
I'm trying to use cfcase with a variable expression, which the documentation says should work, but it doesnt appear to (indeed, it breaks the error mechanism). I have:

<cfset pendingUser="foo">
<cfswitch expression="#getUser.status#">
<cfcase value="#pendingUser#" delimiters=",">
<cfset foo="bar">
</cfcase>
</cfswitch>

and cold fusion generates an error saying that the expression must be constant, but that it cant determine where the error is. If I change the <cfcase> to <cfcase value="foo">, it works fine.
BuckyGoldstein said on Oct 22, 2004 at 11:12 AM :
I'm trying to test both for "equals" and "greater than" in a cfswitch statement. It seems to me I should be able to do:

<cfswitch expression = #redirect.RecordCount#>
<cfcase value = "0">
<!--- Do nothing but show variables.message in the body below --->
</cfcase>
<cfcase value="1">
<cflocation URL="#redirect.newurl#" addtoken="no">
</cfcase>
<cfcase value GT 1>
<cfset variables.multiplelinks=true>
<cfset variables.message="The page you are looking for may have moved to one of the following addresses:">
</cfcase>
<cfdefaultcase>

How come I get the error message "Invalid token &apos;1&apos; found on line 40 at column 50."?
Mike@Jobscience said on Jan 20, 2005 at 10:42 AM :
It appears that <CFCASE> cannot use a component or function scoped variables in it's value expression. The following code sets throw the same compilation error: This expression must have a constant value.

<cfcomponent name="cfcase_test">
<cfset this.var1 = "1">
<cffunction name="test_this">
<cfswitch expression="1">
<cfcase value="#this.var1#">Match!/cfcase>
</cfswitch>
</cffunction>
</cfcomponent>

<cfcomponent name="cfcase_test">

<cffunction name="test_this">
<cfset var1 = 1 >
<cfswitch expression="1">
<cfcase value="#var1#">Match!</cfcase>
</cfswitch>
</cffunction>
</cfcomponent>

However, using an if statement, no error is thrown and the function behaves as expected.

<cfcomponent name="cfcase_test">
<cfset this.var1 = "1">

<cffunction name="test_this">
<cfif this.var1 eq "1">Match!</cfif>
</cffunction>
</cfcomponent>

I assume this has to do with the way Neo's Assembler/Translation engine compiles the switch statement, requiring the value to be known a the time of compilation rather than runtime as with an if statement??

--Mike
carehart said on Sep 22, 2005 at 2:11 PM :
Folks, the docs here suggest that a CFCASE value can be a constant or a "constant expression". Yet nowhere do I find that term defined in the CFMX 6.1 docs. Some have interpreted it to mean an expression that resolves to a constant (such as #'a'#), but any attempt to refer to pound signs in a CFCASE value gets an error in CFMX 6.1. What is meant above by constant expression? (I've noticed the CFMX 7 docs have the same issue, and will post this there for those who don't read it here.)
carehart said on Sep 23, 2005 at 8:16 AM :
I need to update my comment above: a constant expression like #'a'# or even #1+1# does indeed work as a CFCASE VALUE in CFMX (though not CF5). When testing it (before making my last comment), I had a different problem that I misinterpreted.

Still, I'll hope that my identification here of what a constant expression is may help others.

That said, I have to say I still don't see why one would ever use one. If any readers ever think of a practical use, I'm sure others would appreciate hearing of it.

 

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

Current page: http://livedocs.adobe.com/coldfusion/6.1/htmldocs/tags-pa9.htm