Create dynamic variables at runtime in your Value Objects

I am amazed at how much I learn every week. I have been coding Actionscript for a long time and no matter what sometimes you learn the most simple cool things. At least I thought it was cool for something as simple as this. Sometimes no matter how many projects you have been on you just don’t run across situations where you have to use parts of the language so you just never find the little nuggets of goodness that make your coding life a little easier.

Enter dynamic variables

Now if you have used just plain old Object you pretty much know that you can do something like the following.

var obj:Object = {};
obj.var1 = 1;
obj.var2 = “mystring”;
obj[“some crazy var with spaces”] = new Array();

Anyway you get the idea.

I am on a project now where we are working with a lot of very generic objects that come back from server requestes and we are decoding JSON so using Object made a lot of sense to the other developers who are mainly have a lot of javascript knowledge and are learning Flex and Actionscript as they go. Naturally they thought JSON was the best way to go. I can’t really argue with that although I would have gone with using something like Blaze being that all of there backend is Java. That way we could use AMF and strictly typed objects that could be passed back and forth. Anyway … I digress.

In order to make our life a little easier and to make the code on the Flex side more understandable I started moving the team away from using plain objects on the actionscript side into using strictly typed objects or Value Object (VO’s), as I like to refer to them.

For examples sake let’s say that data coming back from the server contains Date and Time information. On the actionscript side I create an object like this.

package
{

   public class TimeVO
   {

      public var start:Date;
      public var end:Date;
      public var name:String; 

   } 

This is all great because you can just pass around the TimeVO object to all the components and you know what type of data it contains and there is less room for errors when creating new components that will be working with time data. 

In some of the components we do some data visulization with some of the Flex Chart components. Long story short, so that the chart that I was working with grouped data in the correct manner all the TimeVO objects needed a dynamic object created with a number value. Basically telling me that all the TimeVO’s belong to a particular “group” or “event” in time. I thought, “Hey, everything in Actionscript extends Object, right? All I should have to do is do something like this”.

var timeVO:TimeVO = new TimeVO();
timeVO[“123234345”] = value;

Not so fast! If you try this you will get a Big fat Error! The simple solution is to make the class “dynamic” like this.

Set the class to dynamic

 package

{

   public dynamic class TimeVO
   {

      public var start:Date;
      public var end:Date;
      public var name:String; 

   } 

Here is the official documentation