Skip to main content
Pouradam
Inspiring
January 14, 2014
Answered

Can we assign "Arrays" as class properties??

  • January 14, 2014
  • 1 reply
  • 601 views

Hi there!

Is that possible we assign an array as the class properties?

Code:

package

{

          import flash.net.URLLoader;

          import flash.net.URLRequest;

          import flash.events.Event;

          import flash.events.EventDispatcher;

 

          public class TextToArray

          {

     public var urlLoader:URLLoader = new URLLoader();

     public var dataArray:Array = new Array();

     public var textURL:String = "";

     public var seperator:String = "";

 

     public function TextToArray(textURL:String,seperator:String)

     {

        this.textURL = textURL;

        this.seperator = seperator;

        urlLoader.load(new URLRequest(textURL));//URL to the text file we need to import to Flash

        urlLoader.addEventListener(Event.COMPLETE, dataArrayLoaded);

     }

     public function dataArrayLoaded(e:Event):void

    {

       this.dataArray = e.target.data.split(seperator);//the seperator such as "\n"

       urlLoader.removeEventListener(Event.COMPLETE, dataArrayLoaded);

       trace(this.dataArray[2]);  // corret Data

     }

     public function getArray():Array{

       trace(this.dataArray[2]);  // undefined

       trace(textURL); // correct Data

       return dataArray;

     }

     public function getArrayLength():int{

        return dataArray.length;

      }

   }

}

What am I doing wrong?

Thanks for your time.

This topic has been closed for replies.
Correct answer Ned Murphy

I believe it will be a matter of calling the function before the data file has had a chance to get loaded and processed.

When declaring your variables you are better off not assigning them values in the declaration like you are doing.  Have the Main constructor function assign value to the Array, wait until that processing is done, and then look for the data.  Have the TextToArray class dispatch an event when it is does processing and have a listener for it assigned in the Main file.  The the event handler for that listener process the data into your quranData array.

1 reply

Ned Murphy
Legend
January 14, 2014

When/where are you calling the getArray function?

Pouradam
PouradamAuthor
Inspiring
January 14, 2014

thanks a lot for your prompt reply! I have called it in another class (Main.as) like this:

package

{

          import TextToArray;

          import VerseID;

          import flash.display.MovieClip;

          public class Main extends MovieClip

          {

      ...

      var textToArray:TextToArray = new TextToArray("data/connection.txt","\n");

      var quranData:Array = new Array();

      ...

                    public function Main()

                    {

      this.quranData = textToArray.getArray();

      trace(this.quranData[2]); // undefined

       ..........

Ned Murphy
Ned MurphyCorrect answer
Legend
January 14, 2014

I believe it will be a matter of calling the function before the data file has had a chance to get loaded and processed.

When declaring your variables you are better off not assigning them values in the declaration like you are doing.  Have the Main constructor function assign value to the Array, wait until that processing is done, and then look for the data.  Have the TextToArray class dispatch an event when it is does processing and have a listener for it assigned in the Main file.  The the event handler for that listener process the data into your quranData array.