EasyCFM.COM ColdFusion Forums / Coding Help! / :noob question: is there a "isnotdefined()" func?

   Reply to Discussion | New Discussion << previous || next >> 
Posted By Discussion Topic: :noob question: is there a "isnotdefined()" func?

book mark this topic Printer-friendly Version  send this discussion to a friend  new posts last

brds
06-25-2007 @ 11:05 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Junior Member
Posts: 50
Joined: Jun 2007

I need to throw a catch statement if a variable is not defined.  Is there a isnotdefined() function or would i have to do something like:


<cfif isdefined("varName")>
    <cfelse>
        Code Here
</cfif>


JJfutbol
06-25-2007 @ 11:11 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

In this case there is no function like that. You can always create your own function called isNotDefined if you like though but not worth the trouble. To simply reverse the result of isDefined you put a NOT before it. For example, to do what you want you would do this:

<cfif NOT isDefined('myVar')>
     <cfthrow message="myVar is not defined">
</cfif>

----------------------------------------------------
Some free CF/XHTML/CSS code available at my site www.javier-julio.com. Currently, I'm planning a new site design and already am working on several new CF projects for the community. They will be free and open source as always. The main projects are an Object Factory and a complete rebuild of the Data Validator.

brds
06-25-2007 @ 11:54 AM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Junior Member
Posts: 50
Joined: Jun 2007

I'm getting an error with that code...

Error:
quote:


Element ROWS is undefined in SESSION



My code is this:


<cfif NOT isdefined(session.rows)>
     <cfset session.rows = #countrows#>
</cfif>


It's kinda weird because the error is stating what i'm asking... i'm asking if there variable is declared, and it's throwing an error b/c it's not... seems kind of redundant.  Any thoughts?

brds
06-25-2007 @ 12:03 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Junior Member
Posts: 50
Joined: Jun 2007

nevermind... i'm a moron.  I got it.

JJfutbol
06-25-2007 @ 12:04 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

In this case you are actually passing the variable value but the error is correct. The variable doesn't exist. What you need to do is pass a string to the isDefined function. So instead of:

<cfif NOT isDefined(session.rows)>

It should be:

<cfif NOT isDefined("session.rows")>

Does the difference make sense? CF parses the cfif and looks for a value in a key called rows in the session scope but doesn't find it hence the error. But what you want to do is test if the variable exists so you can't just assume it does (as in the first cfif). You need to pass it as a string so the function itself will determine if it does or does not exist and return the boolean result.

----------------------------------------------------
Some free CF/XHTML/CSS code available at my site www.javier-julio.com. Currently, I'm planning a new site design and already am working on several new CF projects for the community. They will be free and open source as always. The main projects are an Object Factory and a complete rebuild of the Data Validator.

JJfutbol
06-25-2007 @ 12:05 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Moderator
Posts: 1250
Joined: Nov 2004

Now you tell me after I typed all that. You owe me one brds. Smile  Glad its all working for you. If you have more questions let us know.

----------------------------------------------------
Some free CF/XHTML/CSS code available at my site www.javier-julio.com. Currently, I'm planning a new site design and already am working on several new CF projects for the community. They will be free and open source as always. The main projects are an Object Factory and a complete rebuild of the Data Validator.

brds
06-25-2007 @ 12:18 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Junior Member
Posts: 50
Joined: Jun 2007

Okay, not that i'm not getting an error, it's not working how i would like it.  Here's the scenario:

I have a database with a list of acronyms.  The total number of acronyms is going to be constantly changing (both up and down in count).  When the user first goes to the acronym page, all acronyms are listed.  Because they're all listed, i figured it'd be a good place to get a count of how many there are (to be used with paging).  the user can then select how many results they want per page (either 25,50,100, or all).  For some reason, the code (below) is renaming session.rows with every page load instead of giving the var a name only if it's not defined.


<cfif NOT isdefined("session.rows")>
     <cfset session.rows = #countrows#>
</cfif>


I would think that on the first page load, the browser would hit that code, and say "If 'session.rows' is not defined, make session.rows equal to the number of rows that i just spat out"

but instead, it seems to be reading it like this:  "Forget what brds thinks, i'm going to do what i want and rename the variable every time."

if you would like to see more of the code (i.e. the table output with the row counter... just say so, but that part is working fine.


Thanks again for all your help... i know it must be frustrating trying to help me out... but i really need the help.  This is only my 4th day doing cf

This message was edited by brds on 6-25-07 @ 12:19 PM

brds
06-25-2007 @ 12:50 PM
Reply
Edit
Profile
Send P.M.
My Gravatar!
Powered by Gravatar
Junior Member
Posts: 50
Joined: Jun 2007

Nevermind, figured that one out too... stupid applications.cfm file needing to enable sessions


Website Designed and Developed by Pablo Varando.