8 Replies Latest reply on Oct 7, 2008 8:56 AM by g-s-b

    How would you do it

    wkolcz
      I have an application that takes a login of a username and password. The app consists of a main Application and a number of modules and components. I would like to set the username and have it accessible throughout the application via a 'global' variable. What is the procedure to set it so all the modules and components can access it from the main Application?

      Thanks.
        • 1. Re: How would you do it
          dietmar.paulus
          Hi

          If you call Application.application you always have access to your main Application, also if you call this out of the modules.

          So if you define a public variable in your main Application you can access this via Application.application.<your variable>.

          cheers

          dietmar
          • 2. Re: How would you do it
            wkolcz Level 1
            Would I have to set it as static?

            So if after login I set:

            private var username:String = "steve"

            I can access it as Application.application.username?
            • 3. Re: How would you do it
              wkolcz Level 1
              Would I have to set it as static?

              So if after login I set:

              public var username:String = "steve"

              I can access it as Application.application.username?
              • 4. Re: How would you do it
                dietmar.paulus Level 1
                I think yes you can access it like that, but just try it.

                cheers

                dietmar
                • 5. Re: How would you do it
                  Level 7

                  "projectproofing" <webforumsuser@macromedia.com> wrote in message
                  news:gcfjbq$ae3$1@forums.macromedia.com...
                  >I have an application that takes a login of a username and password. The
                  >app
                  > consists of a main Application and a number of modules and components. I
                  > would
                  > like to set the username and have it accessible throughout the application
                  > via
                  > a 'global' variable. What is the procedure to set it so all the modules
                  > and
                  > components can access it from the main Application?

                  Look at the Singleton design pattern.


                  • 6. Re: How would you do it
                    g-s-b Level 1
                    If you look at "best practices" I think you will find that "global" data elements like that are not good. Rather one should "get or put" a copy of the data where it is needed: events, bindings, etc.

                    And the "singleton" pattern itself seems "depreciated" these days.

                    These are mostly "design pattern decisions" implemented for "clean code" and "testing."

                    greg
                    • 7. How would you do it
                      wkolcz Level 1
                      If you look at "best practices" I think you will find that "global" data elements like that are not good. Rather one should "get or put" a copy of the data where it is needed: events, bindings, etc.

                      Sorry, that answer seems very good, but a little vague. Please explain or send me to an example of how this possible.

                      I am not sure if the Singleton design pattern is needed. I am not worried about multiple instances of a username variable in a single user application.

                      From my understanding is that is good put an instance of an object in a singleton to make it's functions accessible and ensuring only one instance.

                      I am more looking for a static variable that is global.
                      • 8. Re: How would you do it
                        g-s-b Level 1
                        If you must have "global" data elements you must decide on what is your liking.

                        Above Application.pplication.myDataElement will work fine.
                        A Class with data elements as static will also work.
                        And finally, as Amy said, a Singleton Class with public variables will also do.

                        Only one piece of data, user... I would suggest the as dietmar.paulus has, the Application.application public data variable.

                        It should get the job done although it is not pretty.

                        greg