11 Replies Latest reply on Aug 19, 2007 1:28 PM by ntsiii

    Dynamic variables name

    chaigneau
      Hello,

      I'm a beginner and i'm looking from several days to resolve this problem but i don't find the solution. Can someone help me, Please.

      /********************************************
      for each (var art:XML in Rs) {
      var eRef:String = String(art.ref); // Exemple Ref AX51
      Ref.data = eRef; // Exemple Ref AX51
      var aFormula:String = String(art.formula); // Exemple Formula +2A-5B+25C
      Formula.data = aFormula; // Exemple Formula +2A-5B+25C
      ??????? = Formula; // Exemple Dynamique Ref AX51 = +2A-5B+25C
      }

      <mx:Panel title="XML" width="100%" height="100%">
      <mx:Label text="Formula" fontWeight="bold"/> <mx:Text id="AX51"width="100%" height="15"/> // +2A-5B+25C
      </mx:Panel>
      ********************************************/
        • 1. Re: Dynamic variables name
          ntsiii Level 3
          I am not quite following your goal, but you can use "bracket" notation to return a reference to an object, and you can use it on the left side of an assignment if the object is dynamic.

          var sPropertyName:String = "myPropertyName";
          MyDynamicObject[sPropertyName] = "myValue"; //left side
          var sValue:String = MyDynamicObject[sPropertyName]; //right side
          trace(sValue); //traces "myValue"

          Tracy

          • 2. Re: Dynamic variables name
            chaigneau Level 1
            Thanks for your reply. It's help me but i not understand how i can have results of sevral formulas wich i can obtain with the id of each formulas. Thank
            • 3. Re: Dynamic variables name
              ntsiii Level 3
              In my example, think:
              sPropertyName == Formula Id ("formula1"),
              and sValue==Formula Value string ("+2A-5B+25C")

              Does that help?
              Tracy
              • 4. Re: Dynamic variables name
                chaigneau Level 1
                Thank. But it's seem more difficult. This is the code with your inidactions :

                /**************** XML FILE ******************/
                <?xml version="1.0" ?>
                <arts>
                <art>
                <ID>1</ID>
                <ref>AX51</ref>
                <formula>+2A-5B+25C</formula>
                </art>
                <art>
                <ID>2</ID>
                <ref>AW42</ref>
                <formula>+4F-2H+3K</formula>
                </art>
                <art>
                <ID>3</ID>
                <ref>AW43</ref>
                <formula>+9Z+3T+6B</formula>
                </art>

                ........

                </arts>


                /**************** AS ******************/

                var MyDynamicObject:Object = new Object();

                for each (var art:XML in Rs) {
                var sPropertyName:String = String(art.ref);
                // Left side AX51
                AW42
                AW43
                var sValue:String = String(art.formula);
                // Right side +2A-5B+25C
                +4F-2H+3K
                +9Z+3T+6B
                MyDynamicObject[sPropertyName] = sValue;
                // Formula AX51 = +2A-5B+25C
                AW42 = +4F-2H+3K
                AW43 = +9Z+3T+6B
                }


                // **************** IMPORTANT : ******************
                // I need to out put the formula using the name's (ref) formula.
                // so i need to use the name (ref) like ID.
                // I don't know how to pass the information between
                // MyDynamicObject[oRef] to the ID,
                // in order to have the information in the panel like above.
                **********************************************/


                /**************** MXML FILE ******************/
                <mx:Panel title="XML" width="100%" height="100%">
                <mx:Label text="Dynamic Ref"/> <mx:Text id="AX51"/> // +2A-5B+25C
                <mx:Label text="Dynamic Ref"/> <mx:Text id="AW42"/> // +4F-2H+3K
                <mx:Label text="Dynamic Ref"/> <mx:Text id="AW43"/> // +9Z+3T+6B
                </mx:Panel>
                ********************************************/

                Thank for your help.
                • 5. Re: Dynamic variables name
                  ntsiii Level 3
                  <mx:Label text="Dynamic Ref"/>
                  <mx:Text id="AX51" text="{MyDynamicObject['AX51']}"/>

                  Tracy
                  • 6. Re: Dynamic variables name
                    chaigneau Level 1
                    Thank,

                    I have tried by i have no result, i don't know why.

                    Here the complet code

                    /***************** BASE.XML ***********************/
                    <?xml version="1.0" ?>
                    <arts>
                    <art>
                    <ID>1</ID>
                    <ref>AX51</ref>
                    <formula>+2A-5B+25C</formula>
                    </art>
                    <art>
                    <ID>2</ID>
                    <ref>AW42</ref>
                    <formula>+4F-2H+3K</formula>
                    </art>
                    <art>
                    <ID>3</ID>
                    <ref>AW43</ref>
                    <formula>+9Z+3T+6B</formula>
                    </art>
                    </arts>

                    /**************** TEST.MXML ******************/
                    <?xml version="1.0" encoding="utf-8"?>
                    <mx:ApolloApplication xmlns:mx=" http://www.adobe.com/2006/mxml" width="606" height="614" initialize="initializeHandler();">
                    <mx:Script>
                    <![CDATA[
                    // public var MyDynamicObject:Object = new Object();

                    public function initializeHandler():void {
                    var myData:XML = new XML();
                    var XML_URL:String = "data/Base.xml";
                    var myXMLURL:URLRequest = new URLRequest(XML_URL);
                    var myLoader:URLLoader = new URLLoader(myXMLURL);
                    myLoader.addEventListener("complete", xmlLoaded);

                    function xmlLoaded(evtObj:Event):void {
                    myData = XML(myLoader.data);
                    var Rs:XMLList = myData.art;
                    var MyDynamicObject:Object = new Object();

                    for each (var art:XML in Rs) {
                    /*********************************************/
                    var sPropertyName:String = String(art.ref); // Left side AX51
                    // AW42
                    // AW43
                    var sValue:String = String(art.formula); // Right side +2A-5B+25C
                    // +4F-2H+3K
                    // +9Z+3T+6B
                    MyDynamicObject[sPropertyName] = sValue; // Formula AX51 = +2A-5B+25C
                    // AW42 = +4F-2H+3K
                    // AW43 = +9Z+3T+6B
                    /********************************************/
                    }
                    }
                    }
                    ]]>
                    </mx:Script>

                    <mx:Panel title="XML Extraction - ID : 1" width="100%" paddingBottom="10" paddingLeft="10" paddingRight="10" paddingTop="10" height="476">
                    <mx:Label text="Dynamic Ref"/> <mx:Text id="AX51" text="{MyDynamicObject['AX51']}"/>
                    </mx:Panel>

                    </mx:ApolloApplication>


                    Thanks
                    • 7. Re: Dynamic variables name
                      ntsiii Level 3
                      Does that compile at all for you? I see MANY errors in the code.

                      If it does compile, send it to me directly. Perhaps the forum is interfering with the code.

                      If it does not compile for you, fix it first.

                      Tracy
                      • 8. Re: Dynamic variables name
                        chaigneau Level 1
                        Thank for your reply,

                        I saw that there is many errors, sorry. I send all the code below. I yhink that the problem comes from the object's declaration where i don't know to put there and explain why no results appear on the screen. First to compil it , i saw errors that came from object's deckaration. I can compil it , but there is no result.

                        ////////////////////// Base.xml ///////////////////////////
                        <?xml version="1.0" ?>
                        <arts>
                        <art>
                        <ID>1</ID>
                        <ref>AX51</ref>
                        <formula>+2A-5B+25C</formula>
                        </art>
                        <art>
                        <ID>2</ID>
                        <ref>AW42</ref>
                        <formula>+4F-2H+3K</formula>
                        </art>
                        <art>
                        <ID>3</ID>
                        <ref>AW43</ref>
                        <formula>+9Z+3T+6B</formula>
                        </art>
                        </arts>
                        //////////////////////////////////////////////////////////////

                        ////////////////////// Test.mxml ////////////////////
                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:ApolloApplication xmlns:mx=" http://www.adobe.com/2006/mxml" width="606" height="614" initialize="initializeHandler();">
                        <mx:Script>
                        <![CDATA[
                        [Bindable]
                        private var MyDynamicObject:Object = new Object();

                        public function initializeHandler():void {
                        var myData:XML = new XML();
                        var XML_URL:String = "Base.xml";
                        var myXMLURL:URLRequest = new URLRequest(XML_URL);
                        var myLoader:URLLoader = new URLLoader(myXMLURL);
                        myLoader.addEventListener("complete", xmlLoaded);

                        function xmlLoaded(evtObj:Event):void {
                        myData = XML(myLoader.data);
                        var Result:XMLList = myData.art.(ID >= 1 && ID <= 3);
                        for each (var art:XML in Result) {
                        var sPropertyName:String = String(art.ref);
                        var sValue:String = String(art.formula);
                        MyDynamicObject[sPropertyName] = sValue;
                        }
                        }
                        }
                        ]]>
                        </mx:Script>

                        <mx:Panel title="XML Extraction" width="100%" height="476">
                        <mx:Label text="Ref AX51" fontWeight="bold"/> <mx:Text id="AX51" text="{MyDynamicObject['AX51']}"/>
                        <mx:Label text="Ref AW42" fontWeight="bold"/> <mx:Text id="AW42" text="{MyDynamicObject['AW42']}"/>
                        <mx:Label text="Ref AW43" fontWeight="bold"/> <mx:Text id="AW43" text="{MyDynamicObject['AW43']}"/>
                        </mx:Panel>

                        </mx:ApolloApplication>

                        Thanks for your help.
                        • 9. Re: Dynamic variables name
                          ntsiii Level 3
                          The code still won't compile. Have you even run this? I'm getting irritated. If I am going to spend my free time reviewing your code, you need to make sure the syntax is at least valid.

                          While I am running Flex and not AIR, the syntax errors are so severe, I am sure this won't compile under anything.

                          I'll give you one more try, then I am done. Be sure it compiles and runs.

                          Tracy
                          • 10. Re: Dynamic variables name
                            chaigneau Level 1
                            hello,

                            i send to you the complied fiel on your email found on you profil. Sorry for all troubles i'm a beginner. Thank for your help
                            • 11. Re: Dynamic variables name
                              ntsiii Level 3
                              I did not get that, please send the source mxml file again, and post it here. IF IT WILL COMPILE.
                              Tracy