Yeah, that is a good point. The times I would like to use it the most are
more like in situations where

<cfset SOMESTATE="1">
<cfest ANOTHERSTATE="2">

<cfswitch expression="#blarg#">
        <cfcase value="#SOMESTATE#"> ...
</cfswitch>



-----Original Message-----
From: S. Isaac Dealey [mailto:[EMAIL PROTECTED]]
Sent: Monday, October 14, 2002 5:13 PM
To: CF-Talk
Subject: RE: switch-case was RE: BlueDragon (was RE: How is CFMX J2EE
implemented?)


> Since we are on a wish list, I would rather just have the
> ability to use variables in a case statement. Then you
> could do a hack like

> <cfscript>
> myrange="";
> for(i=30; i lt 45; i=i+1){
>       devnull = listAppend(myrange,i,",");l
> }
> </cfscript>

> <cfswitch expression="#myswitch#">
>       <cfcase value="#myrange#" delimiters=","></cfcase>
>       ...
> </cfswitch>

> there have been other instance were a variable in a case
> would have been a life saver.

My suspicion is that this wasn't done for reasons of both performance and
clarity. Not that we can't create horrendous, impossible to read code
anyway, but I can imagine that case statements with variables could become
very confusing very fast.

There is another way to simulate variables or ranges in a case statement
with a cftry and cfcatch blocks...

<cftry>
        <cfif myvariable gt x and myvariable lt y><cfthrow type="switch"
message="1"></cfif>
        <cfif listfindnocase(mylist,myvariable)><cfthrow type="switch"
message="2"></cfif>
        <cfif refindnocase(myregex,myvariable)><cfthrow type="switch"
message="3"></cfif>
        ... default case stuff here ...
        <cfcatch type="switch">
                <cfswitch expression="#cfcatch.message#">
                        <cfcase value="1">... do stuff...</cfcase>
                        <cfcase value="2">... do stuff...</cfcase>
                        <cfcase value="3">... do stuff...</cfcase>
                </cfswitch>
        </cfcatch>
</cftry>

Of course, if you want to reduce the amount of typing here, you can
eliminate the message in your <cfthrow> tags and throw different custom
types and then use a <cfcatch> in place of each case statement, i.e.

<cftry>
        <cfif myvariable gt x and myvariable lt y><cfthrow type="1"></cfif>
        <cfif listfindnocase(mylist,myvariable)><cfthrow type="2"></cfif>
        <cfif refindnocase(myregex,myvariable)><cfthrow type="3"></cfif>
        ... default case stuff here ...

        <cfcatch type="1"> ... do stuff...</cfcatch>
        <cfcatch type="2">... do stuff...</cfcatch>
        <cfcatch type="3">... do stuff...</cfcatch>
</cftry>

It's more typing than the <cfswtich> sure, but not insurmountable, and it
allows you to perform additional processing between your if / case
statements, which isn't available to you with a standard switch-case
structure iirc. I've used try catch for a number of non-error handling items
. properly implemented, it works pretty well. Or at least it has for me.
I'm not sure, but I think you might also be able to get away with using
variables in the type attribute of your cfcatch tags... I've never tested
this tho, so I'm not sure.

How about this for an age range issue?

<cfset myage = 25> My age is
<cftry>
        <cfif myage gte 16 and myage lte 20><cfthrow type="16"></cfif>
        <cfif myage gte 21 and myage lte 35><cfthrow type="21"></cfif>
        unknown
        <cfcatch type="16">between 16 and 20</cfcatch>
        <cfcatch type="21">between 21 and 35</cfcatch>
        <cfcatch></cfcatch>
</cftry>

Now that's not too terribly far off from the equivalent cfswtich statement
is it? ... Granted, you could accomplish something similar with this:

<cfset myage = 25> My age is
<cfif myage gte 16 and myage lte 20> between 16 and 20
<cfelseif myage gte 21 and myage lte 35> between 21 and 35
<cfelse> unknown </cfif>

And this is much shorter, however, with the try block, you can do all kinds
of crazy stuff in the in-between parts...

I'm a
<cftry>
        <cfif myage lt 16><cfthrow type="agerange" message="child and can't
drive"></cfif>
        <cfset candrive = true>

        <cfif myage lt 21><cfthrow type="agerange" message="teenager and you wish I
couldn't drive"></cfif>
        <cfset candrink = true>

        <cfif myage lt 60><cfthrow type="agerange" message="adult and I only drive
after a few beers"></cfif>
        <cfset drivesstraight = false>
        old bastard and the line belongs in the middle of my car

        <cfcatch type="agerange"><cfoutput>#cfcatch.message#</cfoutput></cfcatch>
</cftry>

This is roughly equivalent to the cfscript switch-case functionality which
allows you to cascade your case statements by leaving out the break;
statement at the end of a case block. The only thing that's a bit
counterintuitive about this approach ( at least the  only thin I find
counterintuitive about it ) is that your default case is in the middle
instead of at the end... Though you could remedy this by having a final
<cfthrow> in the middle and a final <cfcatch> with another custom type at
the bottom.

Anyway... I've gone on and on... I think I'll let it drop here...

Isaac
Certified Advanced ColdFusion 5 Developer

www.turnkey.to
954-776-0046



~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Archives: http://www.houseoffusion.com/cf_lists/index.cfm?forumid=4
Subscription: http://www.houseoffusion.com/index.cfm?sidebar=lists&body=lists/cf_talk
FAQ: http://www.thenetprofits.co.uk/coldfusion/faq
Your ad could be here. Monies from ads go to support these lists and provide more 
resources for the community. http://www.fusionauthority.com/ads.cfm

Reply via email to