9 Replies Latest reply: Dec 28, 2006 1:16 AM by Stressed_Simon RSS

    Global CFC Variables

    Dan Bracuk Community Member
      I am looking to make some variables in a cfc available to all the functions in that cfc. I used to do this with properties, but just discovered a problem with that approach. Now I am trying normal variables, but I find that not all of them keep their value. The pattern appears to be, if the variable was set to an arguement it loses it's value, otherwise it keeps it.

      For example
      Calling template:
      args = structNew();
      args.StartDate = "2006-09-01";
      args.EndDate = "2006-09-01";

      abc = CreateObject("component","BlocksAndClosures");
      x = abc.init(argumentcollection = args);
      z = abc.blocks();

      -------------------------------
      Start of cfc
      <cfscript>
      WhatToUse = "not set";
      StartDate = "1990-01-01";
      EndDate = "1993-01-01";
      x = true;
      </cfscript>

      ----------------
      init function code
      StartDate = CreateOdbcDate(arguments.StartDate);
      EndDate = CreateOdbcDate(arguments.EndDate);
      WhatToUse = "something else";

      <cfdump var="#variables#">
      -----------------------
      blocks function code
      <cfdump var="#variables#">

      ------------------------------------
      When I dump the variables at the end of the init function, all the variables have the new values. That is to be expected. When I dump them in the blocks function, WhatToUse now equals "something else" which came from the init function. However, the two date variables have the 1900 and 1993 values which appear to mean that they were reset.

      I can work around this but does anyone know why not all the cfc variables behaved the same when the block function was called?
        • 1. Re: Global CFC Variables
          insuractive Community Member
          Could you be getting a scope collision with the arguments variables and your global variables? What happens if you rename the arguments or the global vars? I've gotten the CFC global variables thing to work before, but I usually make sure that the global variables name differ from my local or input variables.
          • 2. Re: Global CFC Variables
            Dan Bracuk Community Member
            Renaming the global variables made it work. Thank you very much. I take back half the bad things I said about you.

            • 3. Re: Global CFC Variables
              Newsgroup_User Community Member
              Renaming the global variables made it work. Thank you very much. I
              take back half the bad things I said about you.

              OR properly scope all your variables. Use variables.myStuff when you
              want the global variables and this.myStuff when you want public
              variables, arguments.myStuff for arguments extra.
              • 4. Re: Global CFC Variables
                Community Member
                Dan which version of CF are you running? I ran your code in 7,0,2,142559 and did not see what you saw. When dumping the variables scope in blocks(), I saw the "new" values (as set by init()), not the initial ones.

                I cannot state how much I agree with Ian: always scope your variables.

                --
                Adam
                • 5. Re: Global CFC Variables
                  Community Member
                  Ditto 6,1,0,63958.

                  --
                  Adam
                  • 6. Global CFC Variables
                    Dan Bracuk Community Member
                    quote:

                    Originally posted by: Adam Cameron
                    Dan which version of CF are you running? I ran your code in 7,0,2,142559 and did not see what you saw. When dumping the variables scope in blocks(), I saw the "new" values (as set by init()), not the initial ones.

                    I cannot state how much I agree with Ian: always scope your variables.

                    --
                    Adam

                    7.0.2
                    • 7. Re: Global CFC Variables
                      Community Member
                      Err... there is no such version of CF as 7.1.2: 7,0,2,142559 is the latest.

                      Go into CF Administrator, and look at the System Info page.

                      I know that CFMX60 has major problems with CFCs (in particular with variables scopes), and should be avoided. I am unaware of any such problems in latter versions of CF.

                      --
                      Adam
                      • 8. Re: Global CFC Variables
                        Dan Bracuk Community Member
                        quote:

                        Originally posted by: Adam Cameron
                        Err... there is no such version of CF as 7.1.2: 7,0,2,142559 is the latest.

                        Go into CF Administrator, and look at the System Info page.

                        I know that CFMX60 has major problems with CFCs (in particular with variables scopes), and should be avoided. I am unaware of any such problems in latter versions of CF.

                        --
                        Adam

                        7,0,2,142559

                        For the record, I am strictly a developer and don't have access to our admin page. It's a nice situation, I highly recommend it.
                        • 9. Re: Global CFC Variables
                          Stressed_Simon Community Member
                          If you do not have access to the administrator you can see the cf version by dumping the server scope.