« My Slowwww Comment Responses | ^ Main | Moveable Type 3.35 Installed + Akismet »
Flash CS3: Validated Enum Values
I am working on an enum class and I needed a way, within the class, to validate the types. As any great developer would do…I started extracting the validation code. :-) lmbo. (joke @ extraction = great developer…not tooting my horn) :-D
So, the problem is simple…how do you extract validating code for an enum that will obviously change for each time? Oh, keep in mind enums in AS3 don’t exist. You simply use static properties in a class. Here is an example from the AS3 docs:
public final class PrintJobOrientation
{
public static const LANDSCAPE:String = "landscape";
public static const PORTRAIT:String = "portrait";
}
Nothing special there. Now, back to the problem. How do I build a base class to validate the types when I pass them to other classes? In come flash.utils.describeType().
This little function provides A LOT of details about any class and is perfect for what I need/want to accomplish. Here is a sample of the output.
<type name=”myapp.enums::BrowserTypes” base=”Class” isDynamic=”true” isFinal=”true” isStatic=”true”>
<extendsClass type=”Class”/>
<extendsClass type=”Object”/>
<constant name=”IMAGE_BROWSER” type=”String”/>
<constant name=”TEMPLATE_BROWSER” type=”String”/>
<accessor name=”prototype” access=”readonly” type=”*” declaredBy=”Class”/>
<factory type=”myapp.enums::BrowserTypes”>
<extendsClass type=”myapp.enums::EnumBase”/>
<extendsClass type=”Object”/>
</factory>
</type>
To generate this XML, I used the following code:
import flash.utils.describeType;
import myapp.enums.BrowserTypes;
var description:XML = describeType(BrowserTypes);
trace(describeType(BrowserTypes));
That’s it. I read my entire class file and received XML info about it which means I can (duhn duhn duhn duhnnnnnnnnnnnnn [or however horns sound when they blare for you]) PARSE THE XML!! :-)
Let me get to the bottom line…here is the base class I created to validate any enum constant.
/**
* EnumBase
* @author John C. Bland II
* @version 0.1
*/
package myapp.enums {
import flash.utils.*;
public class EnumBase {
public static function isValid(value:String):Boolean{
return describeType(BrowserTypes)..constant.(@name==value).toXMLString().length > 0;
}
}
}
Here is the enum which extends this class.
/**
* BrowserTypes Enum
* @author John C. Bland II
* @version 0.1
*/
package myapp.enums {
public final class BrowserTypes extends EnumBase{
public static const TEMPLATE_BROWSER:String = “TEMPLATE_BROWSER”;
public static const IMAGE_BROWSER:String = “IMAGE_BROWSER”;
}
}
Pretty slick huh? The parsing merely returns a boolean if it can’t find the node with a name that matches the passed in value. :-D YUMMMMMMMMMMMMMMMY!
NOTE
This will only validate constants. You can easily change the EnumBase.isValid() method to parse whatever you want but your enum(s) should use constants anyway.
Posted by John C. Bland II on June 26, 2007 2:17 AM | Permalink
TrackBack
TrackBack URL for this entry:
http://mt.katapultmedia.com/mt-tb.cgi/277
Comments
Great Post
Thanks For The Information
Posted by: islam Ahmed | June 26, 2007 4:04 AM
No sweat Islam. I had some problems with the isValid() working through the subclasses so I am just using EnumBase.isValid(“value”) (which is pretty much the same thing).
If I make anymore progress regarding extending with static methods, I’ll post.
Posted by: John C. Bland II | June 27, 2007 12:16 AM



