2 Replies Latest reply on Feb 19, 2012 3:45 PM by ninjatogo

    Problems with simple conditionals

    ninjatogo

      Hello, I am very new to programming but I have started practicing with AIR and Flashbuilder.

      I'm having a bit of trouble trying to write what seems like a simple conditional statement that works.

       

      My problem is that I have a textbox[a] that may have text in it, when it does have text in it the function I have works.

      I want to pull text from a different textbox[b] when textbox[a] is empty and perform the original function on the text of textbox[b].

       

      My current code:

       




      protected function Replace_clickHandler(event:MouseEvent):void



      {




      // TODO Auto-generated method stub




      var str:String = OutputBox.text;




      var search:String = Replacethis.text;




      var replace:String = Replacewith.text;










      function strReplace(str:String, search:String, replace:String):String {





      return str.split(search).join(replace);




      }










      OutputBox.text = (strReplace(str, search, replace));



      }





       

      Any help would be appreciated!

        • 1. Re: Problems with simple conditionals
          Webtop

          You should do something like this:

           

          var str:String = (Textbox1.text != "") ? Textbox1.text : Textbox2.text;

           

          Replace the references to Textbox1 and Textbox2 with the names of your textboxes.

          That piece of code uses what is called a ternary operator. If the expression in the parentheses is true, the code after the ? is used, otherwise the code after the : is used.

           

          It is a shorthand version of this:

           

          if(Text1.text != "") {

              var str:String = Textbox1.text;

          } else {

              var str:String = Textbox2.text;

          }

           

          They both do the same thing, one is just more concise.

           

          You should also trim any whitespace from the text before testing it, or a single space in Textbox1 with evaluate as true.

           

          Hope that helps.

          Paul

          • 2. Re: Problems with simple conditionals
            ninjatogo Level 1

            Thanks for the reply; I can't test it right now, but I will give it a try.