15 Replies Latest reply on Aug 18, 2008 8:00 PM by G3Joshua

    Var returning NaN in function

    G3Joshua
      I have never had this issue until I starting writing my latest app. This is what it does. When the user logs in it gets a specific ID from the database. Whenever the user clicks on one of the various buttons (that opens pop ups) it carries that variable into the pop up. Simple, that is done. But when the "TitleWindow" loads I have a function that runs on the creationComplete. That code looks like a bit like this:
      ---
      creationComplete="initBuild()"
      ---
      [Bindable]
      public var landID:Number; // the variable coming in from the main application into this popUp.
      ---
      public function initBuild():void {
      if (landID > 0) {
      trace(landID);
      }
      }
      ---
      The problem is that if I put the "landID" into a text field it shows the proper value of 3. But when I run that function is returns NaN as the value not matter. Yeah, I am new and have read a few books but never ran across something like this.
        • 1. Re: Var returning NaN in function
          Level 7

          "G3Joshua" <webforumsuser@macromedia.com> wrote in message
          news:g85p0g$7ch$1@forums.macromedia.com...
          >I have never had this issue until I starting writing my latest app. This is
          > what it does. When the user logs in it gets a specific ID from the
          > database.
          > Whenever the user clicks on one of the various buttons (that opens pop
          > ups) it
          > carries that variable into the pop up. Simple, that is done. But when the
          > "TitleWindow" loads I have a function that runs on the creationComplete.
          > That
          > code looks like a bit like this:
          > ---
          > creationComplete="initBuild()"
          > ---
          > [Bindable]
          > public var landID:Number; // the variable coming in from the main
          > application
          > into this popUp.
          > ---
          > public function initBuild():void {
          > if (landID > 0) {
          > trace(landID);
          > }
          > }
          > ---
          > The problem is that if I put the "landID" into a text field it shows the
          > proper value of 3. But when I run that function is returns NaN as the
          > value not
          > matter. Yeah, I am new and have read a few books but never ran across
          > something
          > like this.

          Sounds like your text field is bound to landID, which means that whenever
          landID happens to get a value it will show up. In your function, I'm
          surprised you see anything at all, because I wouldn't think that NaN would
          be > 0, but, regardless, your function is probably running before your
          application has set the value.

          HTH;

          Amy


          • 2. Re: Var returning NaN in function
            G3Joshua Level 1
            But that is what I do not get. If the function is running after the complete creation of the titleWindow it should have a value set, since the label that I have does reflect a value of 3. Just in the function is when I get a return of NaN. That is what I am not understanding.
            • 3. Re: Var returning NaN in function
              Level 7

              "G3Joshua" <webforumsuser@macromedia.com> wrote in message
              news:g872q4$krc$1@forums.macromedia.com...
              > But that is what I do not get. If the function is running after the
              > complete
              > creation of the titleWindow it should have a value set, since the label
              > that I
              > have does reflect a value of 3. Just in the function is when I get a
              > return of
              > NaN. That is what I am not understanding.

              Put a break point and look at the value at that point.


              • 4. Re: Var returning NaN in function
                G3Joshua Level 1
                The only time I get a non-NaN value back is if the function is ran from a "click" on a button or other component. Yet, if I run the same function on a creationComplete I get a trace value of NaN. This is what puzzles me. The value will display from a click or populate a label but you cannot what so ever run that same function when the popUp loads. Which is what needs to be done in this case.

                I guess I could just make two PopUp components, one for the greater than 0 and one for 0 and load them that way. Just trying to cut down coding. Not sure if that is going to be possible with what I am doing, still to new to all this. Give me PHP and I'm good, lol.
                • 5. Re: Var returning NaN in function
                  Level 7

                  "G3Joshua" <webforumsuser@macromedia.com> wrote in message
                  news:g87njp$buj$1@forums.macromedia.com...
                  > The only time I get a non-NaN value back is if the function is ran from a
                  > "click" on a button or other component. Yet, if I run the same function on
                  > a
                  > creationComplete I get a trace value of NaN. This is what puzzles me. The
                  > value
                  > will display from a click or populate a label but you cannot what so ever
                  > run
                  > that same function when the popUp loads. Which is what needs to be done in
                  > this
                  > case.

                  As I said, it is quite likely that at the time of your creationComplete
                  event, the variable has not yet been set. Did you try placing a break point
                  in the function and looking at the example? You may want to consider that
                  if you're going to come on a forum and ask for advice, if you make it clear
                  that you haven't seriously considered following the advice that the person
                  offering the advice is likely to spend their time on people who don't waste
                  it.


                  • 6. Re: Var returning NaN in function
                    G3Joshua Level 1
                    I guess I was specific enough then. I did do that and at the break point during the function it still returns a value of NaN.

                    I was under the impression from what I have read so far that "creationComplete" means that everything dealing with that application/component is finished. Thus, the variable would have a value. But, that is not the case.

                    Because of it still returning a NaN value at the break point,and assuming that it will never have the value, I have rewritten a lot of the code to check the same variable, but during the PopUp function on the main application. So now if it is 0 it popsUp a different component then if it is 1,2,3 etc.

                    I appreciate your advice, I do; and I took it. But, still doesn't help it make any more sense to me why a value is set at the bottom of a component in a label but not a function that resides above it. Since the label and function are using the same variable. That is what I do not understand.
                    • 7. Re: Var returning NaN in function
                      Level 7

                      "G3Joshua" <webforumsuser@macromedia.com> wrote in message
                      news:g8c7ls$ae0$1@forums.macromedia.com...
                      >I guess I was specific enough then. I did do that and at the break point
                      >during
                      > the function it still returns a value of NaN.
                      >
                      > I was under the impression from what I have read so far that
                      > "creationComplete" means that everything dealing with that
                      > application/component is finished. Thus, the variable would have a value.
                      > But,
                      > that is not the case.
                      >
                      > Because of it still returning a NaN value at the break point,and assuming
                      > that
                      > it will never have the value, I have rewritten a lot of the code to check
                      > the
                      > same variable, but during the PopUp function on the main application. So
                      > now if
                      > it is 0 it popsUp a different component then if it is 1,2,3 etc.
                      >
                      > I appreciate your advice, I do; and I took it. But, still doesn't help it
                      > make
                      > any more sense to me why a value is set at the bottom of a component in a
                      > label
                      > but not a function that resides above it. Since the label and function are
                      > using the same variable. That is what I do not understand.

                      Because the label is probably bound to the data, which means _whenever_ the
                      data happens to show up the label will update. Did you want specific
                      suggestions on how to solve the problem, or were you only interested in why
                      it occurs?

                      -Amy


                      • 8. Re: Var returning NaN in function
                        G3Joshua Level 1
                        At first I was interested in fixing the problem, but when I rewrote the code, I found that I will require a whole lot less code to do the same thing and also be able to keep my components seperate makes it much easier to manage my application. In short, more interested in why it occurs so I can understand ahead of time what I can a cannot accomplish. The adobe training book and the other "PHP and Flex" book doesn't cover stuff like what I am doing now.
                        • 9. Re: Var returning NaN in function
                          Level 7

                          "G3Joshua" <webforumsuser@macromedia.com> wrote in message
                          news:g8cg19$jf3$1@forums.macromedia.com...
                          > At first I was interested in fixing the problem, but when I rewrote the
                          > code, I
                          > found that I will require a whole lot less code to do the same thing and
                          > also
                          > be able to keep my components seperate makes it much easier to manage my
                          > application. In short, more interested in why it occurs so I can
                          > understand
                          > ahead of time what I can a cannot accomplish. The adobe training book and
                          > the
                          > other "PHP and Flex" book doesn't cover stuff like what I am doing now.

                          OK, then I won't suggest that you use a setter function on your variable
                          instead of trying to do critical work in creationComplete ;-)


                          • 10. Re: Var returning NaN in function
                            G3Joshua Level 1
                            Thanks for the help. Can you answer one last question for me?

                            I am trying something a bit different again to try to lighten code up a bit.

                            Can you run another (outside) function from inside a function.

                            One function is used once but I need to reuse it from inside a different function. Is this possible? If so, where should I look for the code. I've tried google and the forums but nothing.
                            • 11. Re: Var returning NaN in function
                              Level 7

                              "G3Joshua" <webforumsuser@macromedia.com> wrote in message
                              news:g8cl5l$p6e$1@forums.macromedia.com...
                              > Thanks for the help. Can you answer one last question for me?
                              >
                              > I am trying something a bit different again to try to lighten code up a
                              > bit.
                              >
                              > Can you run another (outside) function from inside a function.
                              >
                              > One function is used once but I need to reuse it from inside a different
                              > function. Is this possible? If so, where should I look for the code. I've
                              > tried
                              > google and the forums but nothing.

                              Yes, but this breaks loose coupling/encapsulation. A better method is to
                              broadcast an event from the component and then listen for it in the parent
                              document and have the parent run the function.

                              HTH;

                              Amy


                              • 12. Re: Var returning NaN in function
                                ntsiii Level 3
                                I am not quite sure what you are asking. As long as it is in scope, you can call a function from most anywhere.

                                Alos, pay attention to what Amy did not suggest ;) You will have far fewer timing issues if you utilize the built-in event/binding mechanisms than trying to guess when in the component lifecycle your data will be ready.

                                Tracy
                                • 13. Re: Var returning NaN in function
                                  G3Joshua Level 1
                                  What I am now asking...here is a extremely shortened example.

                                  private function land0x0():void {
                                  landID = land00ID;
                                  landLvl = land00Lvl;
                                  landY = 0;
                                  landX = 0;
                                  }

                                  private function loadLand():void {
                                  if (landID == 1) {
                                  ...//load Building 1 PopUp
                                  } else {
                                  ...//load the PopUp allow user to choose building
                                  }
                                  ----
                                  Instead of writing the loadLand if statements for all 16 landYxX() functions just be able to write it in once and depends on which block (0x0,0x1,0x2...) it goes to a function that gives the needed variables the right values then sends those to the loadLand() function which does all the if statements. Since that "if else" function is going to do a whole lot more by the time it is finished. So it will save me tons of coding.
                                  • 14. Re: Var returning NaN in function
                                    Level 7

                                    "G3Joshua" <webforumsuser@macromedia.com> wrote in message
                                    news:g8d439$cm8$1@forums.macromedia.com...
                                    > What I am now asking...here is a extremely shortened example.
                                    >
                                    > private function land0x0():void {
                                    > landID = land00ID;
                                    > landLvl = land00Lvl;
                                    > landY = 0;
                                    > landX = 0;
                                    > }
                                    >
                                    > private function loadLand():void {
                                    > if (landID == 1) {
                                    > ...//load Building 1 PopUp
                                    > } else {
                                    > ...//load the PopUp allow user to choose building
                                    > }
                                    > ----
                                    > Instead of writing the loadLand if statements for all 16 landYxX()
                                    > functions
                                    > just be able to write it in once and depends on which block
                                    > (0x0,0x1,0x2...) it
                                    > goes to a function that gives the needed variables the right values then
                                    > sends
                                    > those to the loadLand() function which does all the if statements. Since
                                    > that
                                    > "if else" function is going to do a whole lot more by the time it is
                                    > finished.
                                    > So it will save me tons of coding.

                                    It would probably save you even _more_ coding to develop a proper data
                                    driven approach, but that's something this forum probably isn't equipped to
                                    teach you post by post ;-)


                                    • 15. Re: Var returning NaN in function
                                      G3Joshua Level 1
                                      Yeah, that is what it seems the more I learn. Just trying to separate my mind from my "normal" php knowledge since this is WAY different.