views:

58

answers:

2

Hi there!

I'm trying to create a dynamicly named object.

something like

private var myVar:String = "dynamicName";
private var [myVar+"staticName"]:Object = new Object;

but what i wrote above dosn't work for some reason.

+1  A: 

You could try something like this:

var myDynamicObjects:Object = new Object();
var myVar:String = "dynamicName";
myDynamicObjects[myVar + "staticName"] = new Object();

Using square brackets allows you to manipulate dynamic object properties.

== Edit ==

Better yet, like @www.Flextras.com says, use a dictionary or an array.

heavilyinvolved
+2  A: 

I'd be very surprised if you get that to work.

Better to save your dynamically created objects in an array or possibly a Dictionary. The Flex Framework uses Arrays for itemRenderers in list based classes, for example. Try something like this using a Dictionary:

private var myObjects : Dictionary = new Dictionary()

In some method where you create the children:

private var myVar:String = "dynamicName";
myObjects[myVar+"staticName"] = new Object();
www.Flextras.com
tried that, getting the error:1078: Label has to be a simple identifier.
masi
remove the :Object from after myObjects[myVar+"staticName"] so that it reads myObjects[myVar+"staticName"] = new Object();
heavilyinvolved
that worked, thanks guys!
masi
Thanks to HeavilyInvoled for catching the typo in my source code. Glad we were able to help @masi
www.Flextras.com