8 Replies Latest reply on Feb 10, 2008 6:13 AM by larry_schwartz

    Access an Application.cfc variable in another CFC

    larry_schwartz
      Hi, I'm setting a variable in Application.cfc to use as a "global constant". I then need to reference it inside a custom <cffunction> in another .cfc file.

      In application.cfc, I use:

      <cfset MyVariable=1>

      Then, in a function in a custom .cfc file, I have something like:

      <cfswitch expression = "SomeValue">
      <cfcase value = #MyVariable#>
      </cfcase>
      </cfswitch>

      #MyVariable# isn't recognized.

      How do I reference #MyVariable#

      (BTW, quoting #MyVariable# or not doesn't help)

      Thank you in advance.
        • 1. Re: Access an Application.cfc variable in another CFC
          Dan Bracuk Level 5
          Make it an application variable.
          • 2. Re: Access an Application.cfc variable in another CFC
            larry_schwartz Level 1
            Thank you, but there's still something wrong. I'm getting the same problem. Referencing the application variable in a cfm page works; in a cfc, it chokes.

            In application.cfc I use:

            <cfparam name="application.MyVariable" default="1">

            Then, I have componont and function something like:
            <cfcomponent>
            <cffunction name="theFunctionName" access="public" returntype="string" >
            <cfdump var = "#application.MyVariable#">
            </cffunction>
            </cfcomponent>

            A call to the the component from cfm page causes the cfm page to choke:

            <cfobject component="theComponentName" name="theComponentNameObj">

            By adding/removing the cffdump and reference to the application variable, it's clearly the application variable that's causing the cfcomponent to fail.

            Thanks again for any help.
            • 3. Re: Access an Application.cfc variable in another CFC
              larry_schwartz Level 1
              Need to update my last post -- the call to cfcomponent does work.

              However, the function chokes when I have any tag that references #application.MyVariable#. The function works as long as I don't have any references to #application.MyVariable#
              • 4. Re: Access an Application.cfc variable in another CFC
                Dan Bracuk Level 5
                quote:

                Originally posted by: larry_schwartz
                Thank you, but there's still something wrong. I'm getting the same problem. Referencing the application variable in a cfm page works; in a cfc, it chokes.


                Thanks again for any help.

                Oh yeah, it would, wouldn't it. cfc's are not part of an application. For global constants, application variables are still a good thing. There are two things you can do. Put a function in your application.cfc that returns the variable. Or, send the variable to your other function as an argument. I'd do the latter myself.
                • 5. Re: Access an Application.cfc variable in another CFC
                  larry_schwartz Level 1
                  Thank you for the info. I didn't realize that.

                  I'll ask my question a different way -- What is the technique for setting up global constants that can be used in all corners of a coldfusion web application? I understand your logic of passing values through arguments or creating functions that return specific values. But if I really need to count on a global constant, is there a way to do it? Is the real answer that I need to readjust my application logic for ColdFusion?
                  • 6. Access an Application.cfc variable in another CFC
                    larry_schwartz Level 1
                    The application variable approach is now working. In application.cfc I put:

                    <cfparam name="application.MyVariable" default="1">

                    In a cffunction in a cfc file I put:

                    <cfdump var="#application.MyVariable#">

                    And it returns the value 1.

                    I really don't know what I changed since my last post. Maybe I was doing something wrong and unintentionally fixed it.
                    • 7. Re: Access an Application.cfc variable in another CFC
                      Stressed_Simon Level 1
                      OK there are a couple of incorrect bits of information that are floating around here. Firstly you can reference application scope inside a CFC but it is not good practice, the best way is to pass anything you need in as an argument.

                      Secondly, I get the feeling that you are not really understanding how the Application.cfc works. The way to think about the Application.cfc is that it is an event listener, so the methods in it only get called when certain events happen. onRequestStart() replaces what Application.cfm used to do by getting called before any other templates get executed. onApplicationStart() get called the first time an application is accessed or the first time it is called after it has been active for more than the applicationtimeout value.

                      So if you have an already running application and add code to the onApplicationStart() method then no amount of clearing template cache will make it execute, as the event it not occurring. It seems to me that you added your cfparam here and expected it to do something. Then your application must have restarted and suddenly started working as you expect.

                      HTH
                      • 8. Re: Access an Application.cfc variable in another CFC
                        larry_schwartz Level 1
                        My situation isn't exactly as you describe, but I agree the issue was probably related to the application "restarting", though I did restart the Windows service a couple of time during my testing, which didn't help my situation. My best guess is success came from fixing my code errors combined with the application restarting.

                        The point of having the equivalent of global constants is that I need set values to which I compare variables. Passing arguments doesnt' work in my situation. User activity creates variables. The variables get passed to a function. I then need to test those variables against fixed values to know how to proceed.

                        If application variables set at the start of the application isn't the way to go, then what is? How does someone set fixed, global contstants?

                        Thank you.