---
id: 657
title: ActionScript 3 Enums
slug: actionscript-3-enums
date: '2008-11-02T21:22:13Z'
modified: '2008-11-02T21:22:13Z'
status: publish
type: post
author: pmoelgaard
categories:
  - ActionScript 3.0
tags:
  - Code Design
excerpt: null
canonicalPath: /posts/actionscript-3-enums
featuredImage: null
commentCount: 8
wordCount: 255
readingTimeMinutes: 2
hasMedia: false
hasVideos: false
url: 'https://forwardthinking.pemo.me/posts/actionscript-3-enums'
---
Based on my [earlier writings](http://blog.petermolgaard.com/2008/10/26/actionscript-enums/) about different ways of faking Enums in ActionScript 3, I decided to do my own based on [Scott's](http://scottbilas.com/2008/06/01/faking-enums-in-as3/) and [Barney's](http://www.barneyb.com/barneyblog/2007/11/02/enums-and-actionscripts-static-initializers/) implementation. Initially I have an Enum.as class from which my application-specific Enums will inherit from...

```jscript
package com.hello.core
{
     import flash.errors.IllegalOperationError;
     import flash.utils.Dictionary;
     import flash.utils.describeType;
     import flash.utils.getQualifiedClassName;

     /**
     * An abstract class to emulate Enum type.
     */
     public class Enum
     {
          /**
          * To protect from instantiation after static initializing.
          */
          protected static var locks:Dictionary = new Dictionary();

          /**
          * Function to call for each enum type declared and in static init.
          */
          protected static function initEnumConstant(inType:Class):void
          {
               var className:String = getQualifiedClassName(inType);
               var typeXML:XML = describeType(inType);
               for each(var constant:XML in typeXML.constant)
               {
                    inType[constant.@name]._label = constant.@name;
               }
               locks[className] = true;
          }

          /**
          * Enum label.
          */
          private var _label:String;

          public function Enum()
          {
               var className:String = getQualifiedClassName(this);
               if(locks[className])
               {
                    throw new IllegalOperationError(”Cannot instantiate anymore: ” + className);
               }
          }

          public function get label():String
          {
               return _label;
          }
     }
}
```

As you can see, the implementation is a very crude combination of Scott's and Barneys implementations. An application-specific Enum could be something called ApplicationState.as, which might look something like this…

```jscript
package com.hello.samples.skinning
{
     import com.hello.core.Enum;

     public class ApplicationState extends Enum
     {
          public static const FINAL:ApplicationState = new ApplicationState();
          public static const INITIAL:ApplicationState = new ApplicationState();

          // static ctor
          {
               initEnumConstants( ApplicationState );
          }
     }
}
```

Please forgive me for not including download links, but the classes are so simple that anyone interested should be able to copy-paste from here.
