13 Replies Latest reply on May 31, 2010 6:39 AM by Rdsingh Ldh

    Passing a Value from One Component to Another

    lkenney2

      I have three actionscript 3.0 classes. The first holds a variable:

       

      package com.ascentify.components.text.variables
      {
           public class TextVariables
           {
                private static var _txtString:String;
                
                public function get textString():String
                {
                     return _txtString;
                }
                
                public function set textString(str:String):void
                {
                     _txtString = str;
                }
           }
      }
      

       

      The second sets the textString value:

       

      package com.ascentify.components.content.functions
      {     
           import com.ascentify.components.text.variables.TextVariables;
           
           import flash.events.Event;
           import flash.net.URLLoader;
           import flash.net.URLRequest;
      
           public class LoadContent
           {
                private var textArray:Array;
                private var xmlLoader:URLLoader;
                
                public static var textVar:TextVariables;
                
                public function LoadContent()
                {
                     xmlLoader = new URLLoader();
                     xmlLoader.addEventListener(Event.COMPLETE, onXmlComplete);
                     xmlLoader.load(new URLRequest("file.xml"));
                     
                     textVar = new TextVariables();
                }
                
                public function onXmlComplete(e:Event):void
                {
                     var _xml:XML = new XML(xmlLoader.data);
                     
                     onContentLoaded(_xml);
                }
                
                public function onContentLoaded(xml:XML):void
                {
                     textVar.textString = xml.toString();
                }
           }
      }
      

       

      And the sets the text box to the textVar.textString value:

       

      package com.ascentify.components.text
      {
           import com.ascentify.components.content.functions.LoadContent;
           import com.ascentify.components.text.components.TextTextArea;
           
           import mx.modules.Module;
           
           import spark.components.HGroup;
           
           public class TextMain extends Module
           {
                //components
                public var txtUnitText:TextTextArea;
                
                public function TextMain()
                {               
                     updateDisplayUnitText();
                }
                
                public function updateDisplayUnitText():void
                {
                     txtUnitText.text = LoadContent.textVar.textString;
                }
      }
      

       

      This, of course, doesn't work. Who do I set the value of a variable in one class and use that value in anoth? Full explaination, please, even if it's just a lik to another site!

        • 1. Re: Passing a Value from One Component to Another
          dasun1981

          You can create a global variable and perform this. You define the global variable in the application level and then access and set values from classes using the FlexGlobals library.

           

          You can create a global variable in the application level,

           

          public var stringName:string;

           

          and then access this value or set this value from other classes or components like this:

           

          import mx.core.FlexGlobals;


          FlexGlobals.topLevelApplication.stringName = "string value";

          • 2. Re: Passing a Value from One Component to Another
            logical_lunacy

            This solution isn't going to work for me. I have a lot of different variables I need to pass between classes, most of them far below the Main Class level. I would just go with what you said, and initialize all of the variables in the Main Class, but I am not the end developer, so I need the final developer to need as little code to create their main class.

             

            Is there a way I can make the class getting the variable to test if the variable is null, possibly using a try, and if it is, go to the class that sets the value and run it?

            • 3. Re: Passing a Value from One Component to Another
              babercan

              Hi,

              Try to use singleton class for accessing variables through your project. Here is an example:

               

              Create a model class contains a text variable named myTextVariable;

               

              package 
              {
                      [Bindable]
                   public class YourModel extends EventDispatcher
                   {
                              public var myTextVariable:String;
              
                        private static var _instance:YourModel;
                        
                        public function YourModel(singletonEnforcer:YourModel)
                        {
                        }
                        
                        public static function getInstance():YourModel
                        {
                             if (YourModel._instance == null)
                             {
                                  YourModel._instance = new YourModel(new SingletonEnforcer());
                             }
                             
                             return YourModel._instance;
                        }
                        }
              }
              
              class SingletonEnforcer{}
              

               

               

              In all of your code , you can get and change the value of the variable like this :

               

              YourModel.getInstance().myTextVariable = "ercan";
              

               

              Hope this helps!

              • 4. Re: Passing a Value from One Component to Another
                logical_lunacy Level 1

                Thanks babercan, I'll try that out.

                • 6. Re: Passing a Value from One Component to Another
                  Rdsingh Ldh Level 1

                  Hi,

                   

                   

                   

                  Does the above code shows any error or what is problem while u are executing it.

                   

                  instead of using

                   

                   txtUnitText.text = LoadContent.textVar.textString;

                  in TextMain Class, have you tried to view the text in the Alert or in the trace.

                   

                  I think it will work for u, only error comes is that where have to added txtUnitText in the applicaiton....................In this class it is not acccesible ...

                   

                  Hopefully this will serve your prupose and still if not..... just send me ur code .... then i will look into it

                  • 7. Re: Passing a Value from One Component to Another
                    logical_lunacy Level 1

                    Yes, I get an error:

                     

                    TypeError: Error #1009: Cannot access a property or method of a null object reference.

                    at com.ascentify.components.text::TextMain/updateDisplayUnitText()[C:\Users\lkenney\Document s\Flex Documents\BetaTest\src\com\ascentify\components\text\TextMain.as:21]

                     

                    LoadContent.textVar.textString reads as a null value.

                    • 8. Re: Passing a Value from One Component to Another
                      Rdsingh Ldh Level 1

                      It is because the object for this class is defined earlier than the loading of the data.....

                       

                       

                      Just do one thing ........ view that text in Alert or trace..... do u still get the error..........If not??

                       

                      Then to over come this solution we can do one thing i.e we need to dispatch the event only when the completion is over that class and then get the data ...........I mean to say that the TextArea is not attached on the screen or in the application and we are refering to it..... so thats y it shows null.

                       

                      I have done that and am just sending you the code hopefully that might work for u.

                       

                      This is mxml class for the application

                       

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()"
                          xmlns:comp="com.ascentify.components.text.*">
                         
                          <mx:Script>
                              <![CDATA[
                                  import com.ascentify.components.text.TextMain;
                                  import com.ascentify.components.content.functions.LoadContent;
                                 
                                  private var objLoadContent:LoadContent = new LoadContent();
                                 
                                  private var objTextMain:TextMain;
                                 
                                  private function init()
                                  {
                                      objTextMain = new TextMain();
                                     
                                  }
                                 
                                  private function clicked():void
                                  {
                                      objTextMain.updateDisplayUnitText();
                                  }
                              ]]>
                          </mx:Script>
                         
                          <mx:HBox id="vbox">
                              <mx:Button click="{clicked()}" />
                          </mx:HBox>
                         
                         
                      </mx:WindowedApplication>

                      • 9. Re: Passing a Value from One Component to Another
                        Rdsingh Ldh Level 1

                        and rest of the clases are same as ur are only change I did was in class TextMain.as

                         

                         

                        package com.ascentify.components.text
                        {
                             import com.ascentify.components.content.functions.LoadContent;
                            
                             import mx.controls.TextArea;
                             import mx.core.Application;
                             import mx.modules.Module;
                           
                             public class TextMain extends Module
                             {
                                  //components
                                  public var txtUnitText:TextArea;
                                 
                                  public function TextMain()
                                  {         
                                          txtUnitText = new TextArea();
                                          txtUnitText.text = "raman";
                                         
                                          Application.application.vbox.addChild(txtUnitText);
                                         
                                          trace("text main class")
                                       //updateDisplayUnitText();
                                  }
                                 
                                  public function updateDisplayUnitText():void
                                  {
                                     
                                    txtUnitText.text = LoadContent.textVar.textString;
                                  }
                             }
                        }

                         

                         

                        This will solve your problem .... and still if u have any feel free to ask me

                        • 10. Re: Passing a Value from One Component to Another
                          logical_lunacy Level 1

                          Hey Rdsingh Ldh,

                          I'm still getting this error:

                           

                          TypeError: Error #1009: Cannot access a property or method of a null object reference.
                          at com.ascentify.components.text::TextModule/updateDisplayUnitText()[C:\Users\lkenney\Docume nts\Flex Documents\BetaTest\src\com\ascentify\components\text\TextModule.mxml:104]
                          at TestTwo/clicked()[C:\Users\lkenney\Documents\Flex Documents\BetaTest\src\TestTwo.mxml:25]
                          at TestTwo/___TestTwo_Button1_click()[C:\Users\lkenney\Documents\Flex Documents\BetaTest\src\TestTwo.mxml:31]

                           

                          Your method didn't seem to help.

                          1 person found this helpful
                          • 11. Re: Passing a Value from One Component to Another
                            Rdsingh Ldh Level 1

                            Jsut send me ur project or the file which show this error..... Then only I will come to solve your problem.....

                            Its working fine on my system................

                            Just tell me one thing.......when are u creating the object for that class..............its after the completion of the application or .....in between somewhere...

                             

                            Just give me the exact idea then only ...... I will let u know the sol.

                            1 person found this helpful
                            • 12. Re: Passing a Value from One Component to Another
                              lkenney2 Level 1

                              Hey Rdsingh Ldh, I've figured out the problem.What happenis in the code that I didn't include in the example is an XML file gets opened.

                               

                              xmlLoader = new URLLoader();
                              xmlLoader.addEventListener(Event.COMPLETE, onXmlComplete);
                              xmlLoader.load(new URLRequest("resources.xml"));
                              

                               


                              If I set the variable before the onXmlComplete event, the value passes with no problem. This issue is the value is passed before the xml file is opened, and therefore before it is set. Do you know anyway to delay getting the variable until the LoadContent variable is finished?