Skip to main content
Inspiring
February 16, 2009
Answered

Variables inside functions...

  • February 16, 2009
  • 2 replies
  • 728 views
I understand the following... the this scope is accessible outside of the component as well as inside the component. the variables scope is accessible to all functions within the component.

You can also use: <cfset var myvar = 1 /> to set a variable that is only availaible in the current instance of a function. When the function completes, the variable is lost. I am assuming that a non-scoped variable is in this "var" scope and therefore non-persistent. Is this true?

Main reason I am asking is that you cannot use the "var" style of naming a variable when using queries and such in a function.

So if I do the following:
<cfquery datasource="dsn" name="testQuery">
...
</cfquery>

The resulting variable of testQuery is only available to the function that ran it correct? It would not be available to any other functions in the component without putting it in the variables scope, correct?

I am pretty sure I am understanding this correctly, just want to make sure. Thanks! Sorry if it sounds like a dumb question...lol
    This topic has been closed for replies.
    Correct answer Newsgroup_User
    > I am assuming that a non-scoped variable is in this
    > "var" scope and therefore non-persistent. Is this true?

    Nope. Same as everywhere else in CF, a non-scoped variable will be created
    in the VARIABLES scope.


    > Main reason I am asking is that you cannot use the "var" style of naming a
    > variable when using queries and such in a function.

    Well: no. But think slightly laterally:

    <cfset var testQuery = "">
    <cfquery datasource="dsn" name="testQuery">


    > I am pretty sure I am understanding this correctly, just want to make sure.
    > Thanks! Sorry if it sounds like a dumb question...lol

    It'd only be dumb if you didn't bother asking it.

    However you can help yourself in these situations by reading up in the
    docs:
    http://livedocs.adobe.com/coldfusion/8/Variables_30.html

    --
    Adam

    2 replies

    Inspiring
    February 16, 2009
    Depending on what you are doing and why, there are some options. You can use the request scope. Or you can set the local variable at the top of the cfc. Or you can use the this scope. Or you can make it a property.
    joshebyAuthor
    Inspiring
    February 16, 2009
    My goal is to make sure that the query result (testQuery) is not persistent outside of the function... as soon as the function running the query is done and returns whatever it was supposed to, I want the query to be gone....

    I think that Adams solution of declaring the var first will work....
    <cffunction ...>
    <cfset var testQuery = "" />
    <cfquery datasource="dsn" name="testQuery">
    ...
    </cfquery>
    <...some code...>
    <cfreturn dataToReturn />
    </cffunction>
    Inspiring
    February 16, 2009
    quote:

    Originally posted by: jeby
    My goal is to make sure that the query result (testQuery) is not persistent outside of the function... as soon as the function running the query is done and returns whatever it was supposed to, I want the query to be gone....

    I think that Adams solution of declaring the var first will work....


    I know it will work.
    Newsgroup_UserCorrect answer
    Inspiring
    February 16, 2009
    > I am assuming that a non-scoped variable is in this
    > "var" scope and therefore non-persistent. Is this true?

    Nope. Same as everywhere else in CF, a non-scoped variable will be created
    in the VARIABLES scope.


    > Main reason I am asking is that you cannot use the "var" style of naming a
    > variable when using queries and such in a function.

    Well: no. But think slightly laterally:

    <cfset var testQuery = "">
    <cfquery datasource="dsn" name="testQuery">


    > I am pretty sure I am understanding this correctly, just want to make sure.
    > Thanks! Sorry if it sounds like a dumb question...lol

    It'd only be dumb if you didn't bother asking it.

    However you can help yourself in these situations by reading up in the
    docs:
    http://livedocs.adobe.com/coldfusion/8/Variables_30.html

    --
    Adam
    joshebyAuthor
    Inspiring
    February 16, 2009
    Thanks for your reply Adam... I had actually though about declaring the variable in the "var" scope before calling the query as you had shown:

    <cfset var testQuery = "">
    <cfquery datasource="dsn" name="testQuery">

    That would then make testQuery exist only during the function call correct? I think that is what you were hinting at anyways...

    I was pretty sure unscoped variables were put in the variables scope, but wasn't sure if that was the case within a CFC as the variables scope is only available within the CFC and not to the entire page request.

    Thanks again!