2 Replies Latest reply on Sep 28, 2011 2:09 PM by William Spence

    What can make a FlexGlobal variable "undefined"?

    William Spence Level 1

      I am writing an AIR app.  I have a group of variables that are needed across all of the major components in my application so I have delcared them in the main application as public, and then import the mx.core.FlexGlobals class into all of my major components.  The variable that I am having trouble with is an ArrayCollection called chapterAC.  I delcare it in my main application like this:

       

      public var chapterAC:ArrayCollection = new ArrayCollection();

       

      I will only be adding objects to this ArrayCollection at the zero level.  ie. chapterAC[0].someValue.  Keeping this in mind, I add elements to the ArrayCollection as follows:

       

      FlexGlobals.topLevelApplication.chapterAC.addItemAt({title1: "My Title", value1: 3, title2:"My Second Title", value2: 4}, 0);

       

       

      So the problem is that when traced, under certain circumstances, the trace is saying that the values are "undefined".  For instance, I may change the value of FlexGlobals.topLevelApplication.chapterAC[0].title1 to "hello" and it will trace that the value is now "hello".  Then I will change the value of FlexGlobals...title2 to something like "friend" and it will trace out that its value is now "friend".  But if I go back and trace title1 again, it should still say "hello" but now it says "undefined".  Going back and tracing title2 should say "friend" but it now says "undefined".

       

      Also, if I give the ArrayCollection some values in one component and trace them, they will trace just fine, but in another component, they will trace as "undefined" and if I go back to the original component, they will trace as "undefined".

       

      Am I doing something wrong with the arraycollection?  Do I need to give each individual element a type?

        • 1. Re: What can make a FlexGlobal variable "undefined"?
          Amy Blankenship Level 4

          Global state will kill you in the long run, unless your project is completely trivial.  The problem with global state is that everything can access it, so it can be almost impossible to figure out what is causing an unexpected change in state.

           

          If you put a break point where your trace statement is, what's the value of chapterAC as a whole?

          • 2. Re: What can make a FlexGlobal variable "undefined"?
            William Spence Level 1

            Amy, I appreciate your feedback.  You actually made me go back and brush up on my debugging skills.  But the solution turned out to be simple, because I am simply a moron It turns out if you use the method addItemAt on an arraycollection, it does not simply replace the item that is already there.  It puts the new item at the index you specify, and moves the existing items down the chain, ALL OF THEM.  So I simply had to stop using the addItemAt method and just declare the values directly.

             

            i.e.

             

            FlexGlobals.topLevelApplication.chapterAC[0].title1 = "New Title";