2 Replies Latest reply on Jun 28, 2006 2:48 AM by Newsgroup_User

    Global variables in AS3

    Level 7
      The documentation for AS3 tells me that a variable set as 'public' will
      be available everywhere.

      If I set public var myvar:String; at the top of a CDATA block in an mxml
      file, myvar is not defined in an imported .as file.

      If I set myvar in the .as file it is not defined in the CDATA block.

      How do I set a variable that is available everywhere in my project?

      Doug
        • 1. Re: Global variables in AS3
          chrisichris
          Make the var also static (public only means that the var can be accessed from outside of the class but you still need an instance of the var):

          [mxml file-name: SomeMXML.mxml]
          <script>
          public static var SOME_VAR:String = "xx";
          </script>

          Than you can access it from everywhere with the name of the class/mxml:
          SomeMXML.SOME_VAR;

          For classes it is the same.

          Hope that helps
          • 2. Re: Global variables in AS3
            Level 7
            Many, many thanks for your help. This works, but I still have a problem.

            This global variable mydata is set to null when created, and then gets
            set in an event handler, but although mydata is set correctly inside the
            event handler, the global variable remains null.

            How can I get the event handler to change the value of the global variable?

            Doug