3 Replies Latest reply on Aug 30, 2007 7:59 PM by HankStanley

    Basic Variable Trouble

    HankStanley
      Hello All, I am new to Flex and I am having some trouble with variables. While I've gotten pretty far on what I'd otherwise like to accomplish, I've resorted to simplifying my problem to a simple scenario. Below is a simple Hello World borrowed from the Adobe site but modified with a string variable. The variable declaration throws a parsing error. If I take out the variable and just have the program spit out "Hello World" instead of the hiWorld variable, it works fine. Can anyone give me a working example, using variables, of this program or point out where I am going wrong?

      Thanks in advance.



      <?xml version="1.0" encoding="utf-8"?>

      <mx:Application
      xmlns:mx=" http://www.adobe.com/2006/mxml"
      viewSourceURL="src/HelloWorld/index.html"
      horizontalAlign="center" verticalAlign="middle"
      width="300" height="160"
      >
      <mx:Panel
      paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10"
      title="My Application"
      >
      var hiWorld:String = "Hello World!";

      <mx:Label text="{hiWorld}" fontWeight="bold" fontSize="24"/>
      </mx:Panel>
      </mx:Application>
        • 1. Basic Variable Trouble
          ctzn99
          You need to have any scripting (i.e. variable declarations, methods) within <mx:Script> tags.

          You almost had it correct. I've modified your example a bit to make it work. Additionally you might want to add the "[Bindable]" attribute in front of your variable declaration.

          You can copy and paste the MXML write from here and run it.
          Best,
          Zach

          <?xml version="1.0" encoding="utf-8"?>

          <mx:Application
          xmlns:mx=" http://www.adobe.com/2006/mxml"
          viewSourceURL="src/HelloWorld/index.html"
          horizontalAlign="center" verticalAlign="middle"
          width="300" height="160"
          >
          <mx:Panel
          paddingTop="10" paddingBottom="10" paddingLeft="10" paddingRight="10"
          title="My Application"
          >
          <mx:Script>
          var hiWorld:String = "Hello World!";
          // next line is an alternate way to declare your var, by adding [Bindable] in
          // front of a var you can make change to that var and any controls that use it
          // will autoupdate to reflect the changes.
          // [Bindable] var hiWorld:String = "Hello World!";
          </mx:Script>
          <mx:Label text="{hiWorld}" fontWeight="bold" fontSize="24"/>
          </mx:Panel>
          </mx:Application>
          • 2. Re: Basic Variable Trouble
            ctzn99 Level 1
            you might also want to add "private" in front of your variable declarations when declared outside of a method just so the flex compiler doesn't throw any warnings.

            Best,
            Zach
            • 3. Re: Basic Variable Trouble
              HankStanley Level 1
              Thanks Zach, I know this seemed like a trivial question but the texts I have and searches I've done online yielded no answers. Hopefully some others find this thread useful in their beginnings.

              Thanks again.