0

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.

webdreamer
  • 2,359
  • 3
  • 23
  • 30

3 Answers3

3

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
  • 1,779
  • 16
  • 23
  • 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 Dec 08 '09 at 20:37
1

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
  • 10,864
  • 5
  • 39
  • 77
1

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.

Joyce
  • 1,431
  • 2
  • 18
  • 33