;

PDA

Click to See Complete Forum and Search --> : [RESOLVED] Dynamic access of object properties


Upchurch
08-09-2007, 10:46 AM
(continued from this thread (http://board.flashkit.com/board/showthread.php?t=740445))

I'm looking for a clean way to set an object's properties without knowing ahead of time what kind of object it is or what the properties will be. For example, the object may be a VBox or HBox, in which case I will be setting height, width, border effects, etc, or it may be a TextField, in which case I will be setting font, fontsize, etc.

I could spend lines and lines of code defining what properties to expect for what objects (not looking forward to that), but I was wondering if there is a dynamic way to handle this. For example, the XML object has an attribute array that contains the attribute names and values for a particular node. You can use the following to dynamically set those attributes:

myXML["id"] = "node1";

Is there something similar for objects, i.e.:

myVBox["height"] = 150;

?

senocular
08-09-2007, 11:43 AM
myVBox["height"] = 150; will work if a) myVBox has the height property or b) myVBox (VBox?) is a dynamic class.

Upchurch
08-09-2007, 12:12 PM
myVBox["height"] = 150; will work if a) myVBox has the height property or b) myVBox (VBox?) is a dynamic class.
You're kidding me. It's just that simple? I assumed it wouldn't be since I didn't see any documentation that talked about that method of property access. That is way too cool.

Just to verify:

myVBox.height is equivalent to myVBox["height"]?

( where var myVBox:VBox = new VBox() )

senocular
08-09-2007, 12:19 PM
thats right.
http://livedocs.adobe.com/flex/201/langref/operators.html#array_access

Upchurch
08-09-2007, 12:30 PM
Damn you, Easily Accessible Information!



(thanks)