6 Replies Latest reply on Aug 6, 2007 3:05 PM by KomputerMan.com

    Simple AS question... Convert a string to a component name

    KomputerMan.com Level 1
      This should be a pretty simple thing to do but I can't think of what to search for when looking for an answer...

      I have a datagrid that when selected compares the datagrid selected value with a value in a componentList array. If the values match I make the componentList[0].ComponentName canvas visible. This is how I am currently doing it but there has to be a better way.

      LitVar.visible = false;
      for(var i:int = 0; i < componentList.length; i++)
      if(reportsGrid.selectedItem.RptCnt == componentList[0].RptCnt)
      if(componentList[0].ComponentName == "LitVar"){
      LitVar.visible = true;

      My question is how do I make the component componentList[0].ComponentName visible?

      The values stored in the componentList[n].ComponentName are names of canvases on my form. I plan on adding about 15 hidden canvases on this form and I don't want to loop through all of those names like the code above does. What I want to do is convert the string in the componentList[n].ComponentName array and make that canvas visible.

      Or in other words how do I rewrite componentList[0].ComponentName.visible = true; so it actually works?

      Thanks in advance for the help!!!

      Have an Ordinary Day...
      KomputerMan ~|:-)
        • 1. Re: Simple AS question... Convert a string to a component name
          peterent Level 2
          The first thing that comes to my mind is that I would store the reference to the component and not its name in the data. That way when you make the selection you automatically have the component.

          The other option is this: if you have the parent component, you can do this: somecomponent[ componentname ]

          You use the component name as an index on the parent component.
          • 2. Re: Simple AS question... Convert a string to a component name
            KomputerMan.com Level 1
            Thanks for the reply Peter!!!

            I am not sure what you mean by, "store the reference to the component and not its name" though. Maybe it will help if I describe what I am trying to do on this page.

            I am building a Report Menuing page. To build this page I have several different reports in a datagrid on the left hand side of the page. When the user clicks on one of the reports I want the selection criteria for that report to appear on the right hand side of the page. Some reports share the same selection criteria, some reports have no selection criteria, and some have multiple criteria. I have a different page to manage the report names, the selection criteria names, and the associations between the selection criteria and reports. The associations between selection criteria names and report names gets passed into the Report Menu page I am building as an array called componentList. When the user clicks on the report name in the datagrid it kicks of a loop of the componentList array. If a match is found between the selected report and a report in the componentList array then the appropriate criteria gets displayed.

            The selection criteria components are all housed in a canvas, the name of the canvas is what gets associated to the report name as mentioned above. For instance the BegEndDates canvas contains two calendar controls so the end user can select the date range for the report. Another selection criteria canvas, FCYearType, contains three components so the user can select fiscal or calendar year, year of the report, and the quarter of the report.

            So back to my original problem… When the user selects a report I can tell that the BegEndDates canvas needs to be made visible. Problem is how do I convert the string “BegEndDates” as stored in the array to BegEndDates.visible so I can set it to true???

            Thanks again for the help!!!

            Have an Ordinary Day…
            KomputerMan ~|:-)
            • 3. Re: Simple AS question... Convert a string to a component name
              peterent Level 2
              What I mean is: don't store "BigEndDates" (the string), store BigEndDates (the component). Say you have this record in your DataGrid:

              { name:"Some Report Name", Rpct:"BigEndDate"}

              What you have for the Rpct field is just the String of the component's name. But if you have the component itself, you can store it:

              { name:"Some Report Name", Rpct:BigEndDate}

              Now maybe you don't have a variable or id that's specific to that report, may be you have list of components. Don't get the component's id or name property, just store the component value.
              • 4. Re: Simple AS question... Convert a string to a component name
                KomputerMan.com Level 1
                Store the actual component? As in the compiled component in the database like a BLOB??? Sorry but this doesn't make sense to me...

                Maybe this isn't as easy as I thought... So basically you are saying I can't recast a string to to manipulate the visible property of a canvas?

                Have an Ordinary Day...
                KomputerMan ~|:-)
                • 5. Re: Simple AS question... Convert a string to a component name
                  peterent Level 2
                  Let's say you have this:

                  <mx:Application ... >
                  <mx:Canvas id="outer" ... >
                  <mx:Button id="b1" label="One" ... />
                  <mx:Text id="t1" text="Two" ... />

                  What you are trying to do is take the String "t1" and get the Text component. You can do this:

                  outer["t1"] because "t1" is the name of a member of the object outer - the Text component is a child of the Canvas.

                  Now let's say you have a DataGrid with the first column as "Button" on row 1 and "Text" on row 2. You want to click on the "Text" row and get to the <mx:Text> component child, t1, of the Canvas named outer. Here's how your dataProvider could look:

                  [ {label:"Button", component:b1}, {label:"Text", component:t1} ]

                  Notice that I didn't put the names of the components in quotes - I used their id - which are variables - as the values for the component field in each item. When you click on the DataGrid you get the item for that row. So now:

                  item.component will be the actual item. If you click on the "Text" row you could do this in the DataGrid's event handler:

                  (item.component as Text).text = "You clicked me"

                  which causes the Text component to change its display from "Two" to "You clicked me".
                  • 6. Simple AS question... Convert a string to a component name
                    KomputerMan.com Level 1
                    The answer to this was provided to me by Jeff Tapper so a BIG thanks Jeff!!! All that had to be done is to take my string and turn it into a component name and then make it visible like so...

                    for(var cc:int = 0; i < componentList.length; cc++)
                    var componentName:String = componentList[cc].ComponentName;
                    this[componentName].visible = true;

                    I knew it was simple...

                    Have an Ordinary Day...
                    KomputerMan ~|:-)