5 Replies Latest reply on Apr 17, 2009 1:55 PM by sstronach1

    Variable Scoping in Flex/Flash

      Both the Flex 2 and 3 compilers have warned me about duplicate variable definitions when the variables are in completely different scopes. This behavior is different than any other language I have used. To verify this behavior I used the following code:

       

      if (1 == 0)

      {

      var test:int = 88;

      logger.info(”test ” + test.toString());

      }

      else

      {

      test = 99;

      logger.info(”test2 ” + test.toString());

      }

       

      The code compiles, runs, initializes the test variable and sets it to 99. The log entry in the (1==0) block, as expected, is not written. To rule out some sort of implicit variable declaration I commented out the line [var test:int = 88;] and the compiler promptly issued an undefined variable error. Any additional perspective on why test gets initialized in this code, or why this would possibly be considered correct behavior would be appreciated.

        • 1. Re: Variable Scoping in Flex/Flash
          m_hartnett Level 3

          The smallest unit of variable scope is within a function.  Unlike Java and C++ Flex does not allow block-level variables which are variables declared within { } in a function.   So even though your variable was defined inside the block it is considered a function level variable.

          • 2. Re: Variable Scoping in Flex/Flash
            Level 1

            Interesting. Easy enough to work around but not what I would have expected.

            • 3. Re: Variable Scoping in Flex/Flash
              Level 1

              Function level scoping makes sense but I still get a duplicate variable definition warning in this code on the variable itemElement. I am a little confused as to why this would be the case.

               

              public function init():void

              {

              this.vbxTable.setStyle("skinBgColor", "#505068");

              var itemElement:XML; //DECLARE VARIABLE ONCE

               

              if (this.tableData.hasOwnProperty("title"))

              {

              if (this.tableData.title.length() > 1)

              {

              var left:Text = newTitle(this.tableData.title[0], "left");

              this.hbxHeader.addChild(left);

              var space:Spacer = new Spacer();

              space.percentWidth = 100;

              var right:Text = newTitle(this.tableData.title[1], "right")

              this.hbxHeader.addChild(right);

              }

              else

              {

              this.hbxHeader.addChild(newTitle(this.tableData.title[0], "left"));

              }

              }

              if (this.tableData.hasOwnProperty("header"))

              {

              var headerX:XML = this.tableData.header[0];

              var header:HBox = newHeader();

              if (headerX.hasOwnProperty("item"))

              {

              for each (itemElement in headerX.item)

              {

              var hItem:Text = newHeaderItem(itemElement);

              if (itemElement.hasOwnProperty("@percentWidth")) hItem.percentWidth = itemElement.@percentWidth;

              header.addChild(hItem);

              }

              }

              this.vbxTable.addChild(header);

              }

              if (this.tableData.hasOwnProperty("row"))

              {

              for each (var rowElement:XML in this.tableData.row)

              {

              if (rowElement.hasOwnProperty("item"))

              {

              var row:HBox = newRow();

              for each (itemElement in rowElement.item)  //MULTIPLE VARIABLE DEFINITION WARNING ?!

              {

              var rItem:Text = newRowItem(itemElement);

              if (itemElement.hasOwnProperty("@percentWidth")) rItem.percentWidth = itemElement.@percentWidth;

              row.addChild(rItem);

              }

              this.vbxTable.addChild(row);

              }

              }

              }

              }

              • 4. Re: Variable Scoping in Flex/Flash
                m_hartnett Level 3

                I just pulled the code into a window and added some code to eliminate errors (added hbx and vbx and tableData and a few dummy functions) and I am getting a clean compile with no warnings.  If you post all the code for the window that would help.  Here is the code I used.

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

                 

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

                <mx:Application

                 

                 

                xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()" xmlns:local="*"

                >

                 

                 

                <mx:Script>

                <![CDATA[

                 

                 

                import

                mx.controls.Spacer;

                 

                 

                import

                mx.controls.Text;

                 

                 

                import

                mx.containers.HBox;

                 

                 

                 

                public var tableData:XML = new XML()

                 

                 

                public function init():void

                {

                 

                 

                this.vbxTable.setStyle("skinBgColor", "#505068"

                );

                 

                 

                var itemElement:XML;

                //DECLARE VARIABLE ONCE

                 

                 

                 

                if (this.tableData.hasOwnProperty("title"

                )) {

                 

                 

                 

                if (this

                .tableData.title.length() > 1) {

                 

                 

                 

                var left:Text = newTitle(this.tableData.title[0], "left"

                );

                 

                 

                this

                .hbxHeader.addChild(left);

                 

                 

                var space:Spacer = new

                Spacer();

                space.percentWidth = 100;

                 

                 

                var right:Text = newTitle(this.tableData.title[1], "right"

                )

                 

                 

                this

                .hbxHeader.addChild(right);

                }

                 

                 

                 

                else

                {

                 

                 

                 

                this.hbxHeader.addChild(newTitle(this.tableData.title[0], "left"

                ));

                 

                }

                 

                }

                 

                 

                 

                if (this.tableData.hasOwnProperty("header"

                )) {

                 

                 

                 

                var headerX:XML = this

                .tableData.header[0];

                 

                 

                 

                var

                header:HBox = newHeader();

                 

                 

                 

                if (headerX.hasOwnProperty("item"

                )) {

                 

                 

                for each (itemElement in

                headerX.item) {

                 

                 

                var

                hItem:Text = newHeaderItem(itemElement);

                 

                 

                if (itemElement.hasOwnProperty("@percentWidth"

                )) hItem.percentWidth = itemElement.@percentWidth;

                header.addChild(hItem);

                }

                }

                 

                 

                 

                this

                .vbxTable.addChild(header);

                }

                 

                 

                 

                if (this.tableData.hasOwnProperty("row"

                )) {

                 

                 

                for each (var rowElement:XML in this

                .tableData.row) {

                 

                 

                if (rowElement.hasOwnProperty("item"

                )) {

                 

                 

                var

                row:HBox = newRow();

                 

                 

                for each (itemElement in

                rowElement.item) {

                 

                 

                var

                rItem:Text = newRowItem(itemElement);

                 

                 

                if (itemElement.hasOwnProperty("@percentWidth"

                ))

                rItem.percentWidth = itemElement.@percentWidth;

                 

                row.addChild(rItem);

                }

                 

                 

                 

                this

                .vbxTable.addChild(row);

                 

                }

                 

                }

                }

                }

                 

                 

                 

                public function

                newHeader() : HBox {

                 

                 

                var

                rItem:Text;

                 

                 

                return new

                HBox();

                }

                public

                 

                 

                function

                newRow() : HBox {

                 

                 

                return new

                HBox();

                }

                 

                 

                public function

                newTitle(r:String, t:String) : Text {

                 

                 

                return new

                Text();

                }

                public

                 

                 

                function

                newRowItem(r:String) : Text {

                 

                 

                return new

                Text();

                }

                 

                 

                public function

                newHeaderItem(r:String) : Text {

                 

                 

                return new

                Text();

                }

                ]]>

                 

                 

                </mx:Script>

                 

                 

                <mx:VBox height="100%" id="vbxTable"

                >

                 

                 

                </mx:VBox>

                 

                 

                <mx:HBox height="100%" id="hbxHeader"

                >

                 

                 

                </mx:HBox>

                1 person found this helpful
                • 5. Re: Variable Scoping in Flex/Flash
                  Level 1

                  Yep - that compiled fine for me and it is functionally identical to my component. This code is part of a much larger system that has been in production for a year with only a few minor issues. I just want to clean up all warnings and known issues before adding major new functionality. I am not sure what is going on - this isn't the only weird behavior I have seen in the Flex IDE. I am just going to ignore the dup variable warnings for now if the variable is unique at the function level or higher.

                   

                  Thanks...