5 Replies Latest reply on May 31, 2010 4:18 AM by kiran7881

    Setting and getting data with the help of ActionScript class

    kiran7881 Level 1

      Hi ,

      I am using ActionScript class to store User Name and Password when the User Logs in

       


      package com
      {
         public class Login
         {
            public function Greeter(initialName:String = "")
            {
            }
           
            public var uname:String;
            public var password:String;
            
         
         }
      }

       

      Inside the Login Form Componet , i am creating the Object for this Login class and setting teh data into it as shown

       

      var logincomp:Login = new Login();

      logincomp.uname = UnameTI.text;
      logincomp.password = PassTI.text;


      I need this data inside another CustomComponent , is it possiblke to access this data inside another componnet .

      Can i do this way ??


      var logincomp:Login = new Login();

      var str:String  = new String();

      str =  logincomp.uname;


      Please advice me


      I Know that it is possible with Registering and Dispatching Evenets
      But However I am  not interested to use EVNETS. I am using FLEX 3

      Please advise me if this is possible .

      THnaks in advance .

       

       

      Thanks for helping me to learn Flex Faster .

        • 1. Re: Setting and getting data with the help of ActionScript class
          BhaskerChari Level 4

          Hi,

           

          Are you using any Singleton class like (ModelLocator class) if so then you can put one instance of your Login class in this class....so that you can access this instance globally in any component(infact through out the application).

           

          Declare one instance of Login object in Singleton class as below

          var logincomp:Login = new Login();

           

          Set the values in one component and you can access the values in other component.

           

          Sample Singleton class as shown below ...

           

          package com.model
          {



          [Bindable]
          public class ModelLocator
          {
            private static var instance:ModelLocator;
           
            public var logincomp:Login = new Login(); 
           
            public function ModelLocator()
            {
             if(instance != null)
             {
              throw( new Error( "there can be only one instance of ModelLocator" ) );
             }
            }
            public static function getInstance():ModelLocator
            {
             if(instance == null)
             {
              instance = new ModelLocator();
             }
             return instance;
            }
          }
          }

           

           

          If this post answers your question or helps, please kindly mark it as such.


          Thanks,

          Bhasker Chari

          • 2. Re: Setting and getting data with the help of ActionScript class
            Dajji Level 3

            The way you are trying to access this will not work. As you are creating a new object, you will get a fresh copy of logincomp.uname. Though I don't know the structure of your application, I believe your login form will go away once the login is successful. In that case, the logincomp object which you created within login form, will also be destroyed. One way of being able to access this user information across application is to declare this logincomp object at the application level and making it accessible to all other components/parts of the application.

            • 3. Re: Setting and getting data with the help of ActionScript class
              Rdsingh Ldh Level 1

              Hi,

               

               

              you can do this by making the

               

              public var uname:String; 

              public var password:String;

               

               

              as a static variables and then using them......... with the reference of the class name.........

               

               

              like u can do

               

               

              public static var uname:String; 

              public static var password:String;

               


              and now  u can set or get the value of these variables by using the class name.


              i.e Login.uname

              Login.password

               

               

              hopefully this will work for u...... and if not then do let me know..... and will give u other sol.


                    

              • 4. Re: Setting and getting data with the help of ActionScript class
                BhaskerChari Level 4

                Hi Dajii,

                 

                It will definetely work but we shoud not instantiate the object with in the Singleton class but instantiate in the component where it is being set.

                 

                So the line in Singleton class should be  public var logincomp:Login;

                 

                So the class should be as shown below.

                 

                package com.model
                {



                [Bindable]
                public class ModelLocator
                {
                  private static var instance:ModelLocator;
                 
                  public var logincomp:Login; 
                 
                  public function ModelLocator()
                  {
                   if(instance != null)
                   {
                    throw( new Error( "there can be only one instance of ModelLocator" ) );
                   }
                  }
                  public static function getInstance():ModelLocator
                  {
                   if(instance == null)
                   {
                    instance = new ModelLocator();
                   }
                   return instance;
                  }
                }
                }

                 

                We can also  access this user information across application by declare this logincomp object at the application level by the below line of code...

                 

                Application.application.logincomp....However if he is already have a Singleton class then he can use the approach otherwise he can go for Application level variable decalration.

                 


                Thanks,

                Bhasker Chari

                • 5. Re: Setting and getting data with the help of ActionScript class
                  kiran7881 Level 1

                  Thanks Dajji ,

                   

                  THis is my concern all about :

                   

                  "The way you are trying to access this will not work. As you are creating a new object, you will get a fresh copy of logincomp.uname."

                   

                  but i think Bhasker has made a good solution for this .