Dynamic class in Flex 3


 

Dynamic class in Flex 3

In flex, you must have created ActionScript classes where properties and methods are declared and defined

In flex, you must have created ActionScript classes where properties and methods are declared and defined

Dynamic Class In Flex

In flex, you must have created ActionScript classes where properties and methods are declared and defined. Flex also let you add properties and methods at run time when you create an object of the class. This all can be achieved using "dynamic" keyword while creating class.

DynamicClass.as

package {

public dynamic class DynamicClass {

public var name:String;

public function displayName():String {

return name;

}

}

}

The above class only has one property "name" and one method "displayName()". The above class has the capability to be set new properties and added functions at run time. For example: In the below mxml, we added new property "place" and one new method "displayPlace()".

Dynamic.mxml

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"

creationComplete="init()">

<mx:Script>

<![CDATA[

import mx.controls.Alert;

public function init():void {

var dynamicClassObj:DynamicClass = new DynamicClass();

dynamicClassObj.name = "Roseindia";

// Adding property to the object of dynamic class at runtime

dynamicClassObj.place = "New Delhi";

// Adding method to the object of dynamic class at runtime

dynamicClassObj.displayPlace = function():String {

return this.place;

};

Alert.show(dynamicClassObj.displayName()+", "+ dynamicClassObj.displayPlace());

}

0

]]>

</mx:Script>

</mx:Application>

1

The output of the above application is as below:

Ads