5 Replies Latest reply on May 15, 2010 2:48 AM by BhaskerChari

    help: calling function that calls another function

    iamelleL

      public var sourceFile:File = File.applicationDirectory.resolvePath("serverIP.txt");

      public var fstream:FileStream = new FileStream();

      public var svrAdd:String = '';


      private function readText(e:Event):void {

          svrAdd = fstream.readUTFBytes(fstream.bytesAvailable);

          fstream.close();

      }


      public function get vrieniHttpURL():String

      {

       

         do{

           fstream.addEventListener(Event.COMPLETE, readText); //I want the readText function to be executed first before proceeding

           fstream.openAsync(sourceFile, FileMode.READ);    // because I want to get the new value of svrAdd

         }while(svrAdd == '')

       

         var v:String = 'http://'+ svrAdd +'/zion/php/';

         return v;

      }

       

      PLEASE HELP ME

        • 1. Re: help: calling function that calls another function
          BhaskerChari Level 4

          I dint get what is your problem...Can you please elaborate it ...?

          • 2. Re: help: calling function that calls another function
            iamelleL Level 1

            public function get vrieniHttpURL():String

            {

             

               do{

                 fstream.addEventListener(Event.COMPLETE, readText);

                 fstream.openAsync(sourceFile, FileMode.READ);

             

            //delay execution

             

               }while(svrAdd == '')

             

              var v:String = 'http://'+ svrAdd +'/zion/php/';                 

               return v;

            }

             

             

             

            my problem is, the readText function is not executed. supposedly the readText function will give a new value to the svrAdd variable which is a string.

            but instead, the programs executes

            var v:String = 'http://'+ svrAdd +'/zion/php/';

            return v;

             

            first then goes to the readText function. is it possible to "delay" the do while loop just to make sure that the readText function is executed?

            • 3. Re: help: calling function that calls another function
              BhaskerChari Level 4

              Hi,

               

              First of all please go through the Flex documentation about what is aynchronous operation is..? I think you are not familiar with that concept...well let me explain....

               

              public function get vrieniHttpURL():String

              {

               

                 do{

                   fstream.addEventListener(Event.COMPLETE, readText);

                   fstream.openAsync(sourceFile, FileMode.READ);

               

              //delay execution

               

                 }while(svrAdd == '')

               

                var v:String = 'http://'+ svrAdd +'/zion/php/';                 

                 return v;

              }

               

              In the  function which you posted the looping is unnecessary it will loop continuosly and will defenitely go into an infinite loop because svrAdd remain empty string("") how many time you loop...this is because fstream.openAsync(sourceFile, FileMode.READ);  is an asynchronous operation means the redaText function gets executed only when the textfile is read completely in the mean time the application doesn't wait for the function to call instead it will execute the next lines of code in the function vrieniHttpURL i.e;

               

              var v:String = 'http://'+ svrAdd +'/zion/php/';                 

                 return v;

               

              lines get executed and your return value will be 'http://zion/php/' instead of http://yourdomain/zion/php/

               

               

               

              Use the below code to get the solution:

               

              import mx.core.Application;
                
                 public var sourceFile:File = File.applicationDirectory.resolvePath("serverIP.txt");
                 public var fstream:FileStream = new FileStream();
                 public var svrAdd:String = '';

                 private function readText(e:Event):void {  
                     svrAdd = fstream.readUTFBytes(fstream.bytesAvailable);  
                     fstream.close();
                     svrAdd = 'http://'+ svrAdd +'/zion/php/';
                 }
                 public function vrieniHttpURL():void 
                 { 
                      fstream.addEventListener(Event.COMPLETE, readText); //I want the readText function to be executed first before proceeding 
                      fstream.openAsync(sourceFile, FileMode.READ);    // because I want to get the new value of svrAdd      
                 }

               

               

              So whats happening in above code is once when the function "vrieniHttpURL" is called you are opening the file in read mode and you cannot get the value for the svrAdd within this function since the file contents have been not yet read....the contents within the file are read only after the readText function gets executed...so once your readText function get executed you will have your svrAdd populated with correct value..i.e; it contains your correct address. So after that you can use the svrAdd value...you cannot return the value of svrAdd with in the  "vrieniHttpURL" function as you did in ur code.

               

              Give it  a try....

               

              Actually I am unable to understand why you are doing this much of unnecessary stuff...you can simply store your domain address in a variable and access it in your application as below.

               

              public var svrAdd:String = http://yourdomainname.com/;

               

              whether your domain name will change dynamically...?

               

              Please mark it as answered if it helped you.....

               

              Thanks,

              Bhasker Chari.S

               

               

               

               

               

               

              • 4. Re: help: calling function that calls another function
                iamelleL Level 1

                thank you very much! sorry for causing too much trouble!

                • 5. Re: help: calling function that calls another function
                  BhaskerChari Level 4

                  Hi,

                   

                  Whether this solution worked for you...which method did you followed..?

                   

                  Thanks,

                  Bhasker Chari