views:

922

answers:

1

Hello, I have to create several vbox-es in a for each loop. Now I want to do something like this.

formsArray["vb"+counter] = new VBox; formsArray["vb"+counter].visible = true;

add labels etc.

I can't get this thing to work. Anybody any idea how to create dynamic variable names for my vbox-es?

Thanks

A: 

First off, to use an associative array, you need to use an Object and not an Array (perhaps you already are, then never mind).

You can achieve what you want to do the following way:

var vbox:VBox;
var formsArray:Object = new Object();
var counter:int = 0;

for each(<statement>)
{
    vbox = new VBox();
    formsArray[("vb" + counter.toString())] = vbox;
    counter++;
}

The VBox's visible property is true by default, so no need to explicitly set it.

Answer to additional question in comments:

You don't really need to make use of dynamic references to do what you want to do. You'd be best of creating a custom component for this, extending the VBox class, by creating a new MXML class with VBox as the root tag. Something along these lines:

<mx:VBox ... >

    <mx:Button ... click="btnClickHandler()"/>
    <mx:Script>
        <![CDATA[

            // Toggles visibility of the VBox
            private function btnClickHandler():void
            {
                 visible = !visible;
            }

        ]]>
    </mx:Script>

</mx:VBox>

Then you can just instantiate as many of these custom VBox:es as you need. However, making the VBox invisible will make the contained button invisible as well, making it difficult to click it again. :) You probably want to address that. Anyways, I hope this will point you in the right direction.

Stiggler
thank you for your quick reply, not sure if it works because:I add a btn to the vboxbtn.addEventListener(MouseEvent.CLICK, function ():void {openEdit(formsArray[("vb" + counter.toString())] ), false);When i press the button I want to (for example) see the visible state.So I do something likeprivate function openEditAccount(vb2:VBox ):void { Alert.show (vb2.visible.toString());}What am I doing wrong?
I'm sorry, but I don't quite understand your comment. I take it you're used to coding in JS, judging by the way you're trying to add a dynamic event handler function. This is probably not the best way for you to do things. Could you please explain what you're trying to do and elaborate a bit on the problem you're having? You may even want to create anew question for that. Your original question should be entirely solved by the answer provided.
Stiggler
* i create dynamic vbox-es* every vbox has a button* when you press the button you have to go to a function (not in JS) that makes your vbox visible or invisible* so i want to give the function a parameter with the vbox name, or else I don't know which button is pressed. (that is why i do the strange thing with the MouseEvent.CLICK, function ():void {openEdit(formsArray[("vb" + counter.toString())] ), falseI hope you understand me... i'm kinda lost.With kind regards, Jaq
I updated the answer with an approach that should hopefully achieve what you want.
Stiggler
thank you, I have to do other things then the visibility, it was just an example. I figured out if I had something for that I know which vbox I'm using. Thank you for your time anyway!