5 Replies Latest reply on Jul 20, 2007 10:59 AM by Newsgroup_User

    trouble with "if" statement

    everynewday Level 1
      I'm completely stumped here. I'm sure I'll feel stupid when I find out what's wrong. Here's the code:

      var type:String = theTarget.type;
      trace(type);
      if (type == "admin") {
      admin = true;
      trace(admin);
      }

      the "type" variable comes from a php script through a LoadVars. The second line traces "admin", but the "if" statement fails to execute. Any idea why?
        • 1. Re: trouble with "if" statement
          Level 7
          everynewday,

          > Here's the code:
          >
          > var type:String = theTarget.type;
          > trace(type);
          > if (type == "admin") {
          > admin = true;
          > trace(admin);
          > }

          Okay.

          > the "type" variable comes from a php script through a
          > LoadVars. The second line traces "admin", but the "if"
          > statement fails to execute. Any idea why?

          Use typeof and/or instanceof with type to see what sort of variable it
          is (e.g., is it really a string?). I expect that it should be. You could
          also simply use this ...

          if (theTarget.type == "admin") { ... // etc.

          No need, really, to create a new variable. Still, I'd be curious, too,
          in your shoes. Has your admin variable (the Boolean) been declared
          elsewhere?


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


          • 2. Re: trouble with "if" statement
            everynewday Level 1
            using "typeof" in my trace statement traces "string"
            and I tried "if (theTarget.type == "admin") {" as well, which also didn't work.
            admin is declared globally at the bottom of the first frame (which is where the rest of this code is).
            • 3. Re: trouble with "if" statement
              Level 7
              Hi everynewday,

              The string may not actually be "admin". It may be "admin " or "admin
              ". Try this test:

              var type:String = new String();
              type=theTarget.type;
              trace('String is '+type.length+' characters long'); //Should be 5. If
              not, strip outside space and try again.

              Regards,
              Patrick


              everynewday wrote:
              > I'm completely stumped here. I'm sure I'll feel stupid when I find out what's
              > wrong. Here's the code:
              >
              > var type:String = theTarget.type;
              > trace(type);
              > if (type == "admin") {
              > admin = true;
              > trace(admin);
              > }
              >
              > the "type" variable comes from a php script through a LoadVars. The second
              > line traces "admin", but the "if" statement fails to execute. Any idea why?
              >

              --
              http://www.baynewmedia.com
              Faster, easier, better...ActionScript development taken to new heights.
              Download the BNMAPI today. You'll wonder how you ever did without it!
              Available for ActionScript 2.0/3.0.
              • 4. Re: trouble with "if" statement
                everynewday Level 1
                That was it. I dunno why, but it's actually coming in as "admin " (with the space). The space is not in the database entry, and none of the code adds the space. Weird.

                Thanks for the help
                • 5. Re: trouble with "if" statement
                  Level 7
                  Es nada.

                  Good luck,
                  Patrick

                  everynewday wrote:
                  > That was it. I dunno why, but it's actually coming in as "admin " (with the space). The space is not in the database entry, and none of the code adds the space. Weird.
                  >
                  > Thanks for the help

                  --
                  http://www.baynewmedia.com
                  Faster, easier, better...ActionScript development taken to new heights.
                  Download the BNMAPI today. You'll wonder how you ever did without it!
                  Available for ActionScript 2.0/3.0.