views:

100

answers:

3

Suddenly Flex seems to dislike variable declaration. For example I write (on the script part of a mxml component)

    <mx:Script>
     <![CDATA[
      import mx.collections.ArrayCollection;


      var i:int = 1;
      while(i< 9) i++;

      [Bindable]
        public var evolution:ArrayCollection = new ArrayCollection();


     ]]>
   </mx:Script>

And it says the variable i has not been defined. This doesn't make any sense to me. Any guess of what might have gone wrong? It happened all of a sudden, when I put the evolution ArrayCollection calling the simple constructor with no arguments. I wanted to add items using a while cycle instead, but now I've erased nearly all the code and I can't figure out what went wrong it doesn't seem to recognize my variables anymore! I'm going crazy.

+3  A: 

If you wrap your loop in a function, you'll get past this issue.

As a matter of fact, anytime you try to run code outside of a function you'll get an error like this.

For example, if you added some code setting the .source property of the evolution ArrayCollection, like so:

evolution.source = [1, 2, 3];

then you will get an error at that line telling you that 'evolution' is undefined.

Hope that helps.

Ross Henderson
Lol, that was a bit stupid of me. Thanks, I think it would have take me a while to notice it myself, coding something like Actionscript inside XML still confuses me at times.
webdreamer
+1  A: 

It's not telling you that variable i is not defined, it's telling you that the property i is not defined.

I don't think you can run that while loop outside of an actual function. And really, there wouldn't be a reason too. If you need to run that loop immediately you can put it in the initialize function.

invertedSpear
+1  A: 

Although in the mxml file, you see a lot of xml tags, but when the mxml file is being compiled, it gets translated into a class. Therefore, it's not possible to just write some code in the class that isn't in a function.

joyceschan
Thanks for that info. I knew you couldn't do it, I wasn't sure why.
invertedSpear