webdreamer
webdreamer

Reputation: 2499

Story of a mysterious bug Flex/ Actionscript

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.

Upvotes: 0

Views: 157

Answers (3)

Joyce
Joyce

Reputation: 1451

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.

Upvotes: 1

invertedSpear
invertedSpear

Reputation: 11054

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.

Upvotes: 1

Ross Henderson
Ross Henderson

Reputation: 1779

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.

Upvotes: 3

Related Questions