4 Replies Latest reply on Mar 1, 2008 6:34 AM by GerryIsHere

    Using a variable value

    GerryIsHere Level 1
      Hi there!

      Please, first, promise not to laugh!

      In the script, I wrote:
      text_field.text = "yes";
      var a:String = text_field.text;

      At the beginning of a conditional logic statement I want to use the value of this variable.
      How should I write the condition?

      I tried:
      if (a == yes) {....
      if (a = yes) {....
      if (a == "yes") {....
      if (a.length = 3) {....

      Nothing works...
      Maybe I'm too tired and... confused!
      Please, put me back on the track.

      I thank you in advance.

      Best regards from Old Gerry
        • 1. Re: Using a variable value
          Level 7
          GerryIsHere,

          > In the script, I wrote:
          > text_field.text = "yes";
          > var a:String = text_field.text;

          I'm with you so far.

          > At the beginning of a conditional logic statement I want
          > to use the value of this variable.
          > How should I write the condition?

          The a variable is typed as a string, so its value must be a string.

          > I tried:
          > if (a == yes) {....

          Because "yes" does not appear in quotes, Flash will interpret it as a
          variable. Since you haven't declared a yes variable anywhere, it's value
          will be undefined or null, so your first condition compares a with, say,
          undefined, which evaluates to false.

          > if (a = yes) {....

          This isn't a comparison at all, actually. The single equals sign is an
          active operator that *sets* the value of one thing to another. Here, you're
          setting the value of a to, again, undefined (or perhaps null).

          > if (a == "yes") {....

          This should do it. When you say nothing works, how are you determining
          that?

          > if (a.length = 3) {....

          Again, the single equals sign is killing it. You're trying to set the
          String.length property to a number, but the length property is read-only.

          Is it possible that text_field isn't a correct reference to one of your
          text fields? One of your text fields would have to have the instance name
          "text_field" (minus quotes) in order to be referenceable by ActionScript.


          David Stiller
          Co-author, Foundation Flash CS3 for Designers
          http://tinyurl.com/2k29mj
          "Luck is the residue of good design."


          • 2. Re: Using a variable value
            GerryIsHere Level 1
            Hi David!

            Thanks for your reply and your... solidarity!

            I was so tired, confused and angry that I even tried solutions which I really knew they were wrong.

            I fully agree with your solutions and the related explanations.
            But it still doesn't work!!!
            (I used :
            if (a == "yes") {....
            and
            if (a.length == 3) {.... )

            Problem must be located somewhere else in the script...
            (but certainly not in the name of the TextField as I verified this point)

            Best regards
            Gerry
            • 3. Re: Using a variable value
              Level 7
              Gerry,

              > I fully agree with your solutions and the related explanations.
              > But it still doesn't work!!!

              Let's try this.

              trace(text_field);
              trace(text_field.text);
              trace(a);
              trace(typeof(a));

              Let me know what you see in the Output panel when you test your SWF.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Using a variable value
                GerryIsHere Level 1
                Sorry for wasting your valuable time!
                I was very ttired and confused.

                Problem was in fact located in a misplaced if/else statement which made the script unable to reach the point where I was using the so called "faulty" variable comparison...

                Thank you all for your comprehensive help!