Enums in AS3 / Flash / Flex?

One thing I really like about AS3 over AS2 is how much more compile-time type-checking it adds. However, it seems to be somewhat lacking in that there is no type-checked enumeration structure available. What's a good (best / accepted) way to do custom enumerated types in AS3?

-------------Problems Reply------------

your answer after the jump :-)

Enumerations with classes

Just wanted to share my way

I know, this is a little outdated and does not exactly answer your question, but you might wanna check out haXe. You can also use it to generate as3 for you, plus there are many other reasons to use it. But this'd really get off topic...

I recently discovered that as3commons library has a good base helper class Enum for enums implemetation.

In order to be a true enum it needs to both:

  • Enforce type safety
  • Prevent rogue instances

Few of the simple solutions do both, and the base classes that do are overly complex IMO.

My current favourite is the following style - safe and simple, and shouldn't confuse anyone:

public final class FruitEnum {
private static const CREATE:Object = {};

public static const APPLE:FruitEnum = new FruitEnum(CREATE);
public static const ORANGE:FruitEnum = new FruitEnum(CREATE);
public static const BANANA:FruitEnum = new FruitEnum(CREATE);

public function FruitEnum(permission:Object) {
if (permission !== CREATE){
throw new Error("Enum cannot be instantiated from outside");

CAVEAT: I have seen rare circumstances where a variable initialisation reads an enum const before it set, but in those cases the problem applied equally to other const-based enum emulations.

Category:actionscript 3 Views:0 Time:2008-12-01

Related post

  • Embedding wav files in AS3 Flash/Flex project? 2010-05-13

    The Flash IDE is capable of embedding many types of uncompressed sound files, including wav, and offers optional compression when publishing. However, the [Embed] tag, only seems to allow embedding of mp3 files. Is it truly impossible to embed an unc

  • Good or Bad Examples of Flash/Flex Technology Use 2009-12-26

    I'm a PHP programmer who's learning more about adobe products including flex, and would like to train myself to recognize when flash/flex is the right technology to use for a client/project (and of course when it's not the right technology to use). I

  • Acoustic Echo Cancellation in Flash/Flex using native libraries 2010-02-04

    I have to implement AEC in Flex video conferencing application. I know that it can not be done directly in Flash. I'm thinking to write a native application or library that will do AEC outside Flex. As far I know Adobe Connect Pro is using some exter

  • How to define enum in as3? 2010-03-06

    Is there a way to define an enum in AS3 in a way we do it in other languages? I can define constants with defined values like that: private const CONST_1:int = 0; private const CONST_2:int = 1; private const CONST_3:int = 2; and so on. If I want to i

  • How should a programmer go about getting started with Flash/Flex/ActionScript? 2010-03-21

    What is the shortest path between zero (ie no flash related development software on my computer or information about where to obtain it or get started) to running a "hello world" ActionScript? I'm hoping for an answer that gives step by step instruct

  • Should I be learning Flash/Flex/ActionScript or HTML/CSS/JS ("HTML5")? 2010-09-07

    I'm fairly new to this whole web-development thing (2 weeks maybe, my 1st scraping of code exists here, I quite like it) and I ended up learning to use Adobe's Flash Builder 4, which I have come to quite like (although Action Script annoys me at time

  • Flash / Flex virtual tour of room 2010-09-10

    I want to make a virtual tour of a room in Flash / Flex. How can this be achieved? Firstly, is it enough to have pictures taken from a still point? If yes, how can I turn them into a real tour? Also, is there a library for this kind of thing for AS3?

  • Is it possible to use MongoDB with flash/flex? 2010-10-13

    I would like to know if it is possible to connect MongoDB with Flash/Flex? If yes, kindly advise how could this be done? Thank you. --------------Solutions------------- I dont think there is straight AS3 bindings for mongo. This post explains how to

  • Flash/Flex Rails remoting. AMF, WebORB, not quite sure what to use 2010-12-21

    I'm trying to build a game that sends game play data to a rails app. I've built it in javascript but it's quite obvious that Flash is going to do a better job so now I am about to build it in Flash. Using projects sprouts which is a ruby wrapper to b

  • Flash/Flex: Integrating subversion revision into exe/swf file? 2011-04-15

    I recently I followed this great guide to integrate the subversion revision into exe/dll files generated from my c++/c# visual studio projects. Now I can easily right-click on an exe-file to find which revision was used to build the binary (see image

  • Flash/Flex Speex audio decode, to play a speex file 2011-12-07

    I want to play *.spx files which encoded by Speex on the Web. But I have no knowledge of Flash/Flex or any Flash Audio codec. After Google search for a whole day, I got some solutions, that is: Do something wrapping Speex files with a FLV container b

  • Does anyone know of a php framework that would handle progressive enhancement for Flash/Flex content? 2008-10-03

    Ok, I'm using the term "Progressive Enhancement" kind of loosely here but basically I have a Flash-based website that supports deep linking and loads content dynamically - what I'd like to do is provide alternate content (text) for those either not h

  • What tools to use for developing flash/flex based touch screen user interface for embedded system 2008-10-27

    We are looking at developing a device with a touch screen and an embedded PC like computer for the user interface. What are the benefits and disadvantages of using flash/flex for this user interface development? One of our goals is to seperate the de

  • Image loading icon for flash/flex? 2008-11-07

    You often see those little 'petal' animations while an image or .swf file is loading in a Flash / Flex application. Do I have to steal this from someone or is there a standard one available from somewhere. i would think there is somethin standard bec

  • What's all this business about Flash, Flex, Adobe Air, Java FX and Silverlight? 2008-12-30

    What's all this business about Flash, Flex, Adobe Air, Java FX and Silverlight? Why would I choose one over the other? and what happened to Java Applets and ActiveX controls? Oh, and where does AJAX fit in to all this? and is Laszlo relevant? Aftered

  • Image Maps with Flash/Flex & jQuery 2009-03-11

    Is there anyway to create an image map in Flash/Flex, and have it as a scriptable element in the DOM through jQuery? I currently have a standard HTML image map that, when a certain selection is clicked, checks a checkbox on a form on the same page (v

  • Can Flash/Flex be used to create a Windows Explorer type file tree browser? 2009-08-05

    I'm interested in using Flash or Flex to create a File Explorer/Tree which the user can point to a directory on their hard drive, and keep open while they do other things. Similar to TextMate's project drawer, or the file trees on many code editors.

  • convert java awt applet to flash / flex 2009-10-15

    what's the shortest path to convert a java applet to flash / flex ? have you any experiences with something similar? please describe. thanks guys --------------Solutions------------- I'm in the middle of a project like that right now. There doesn't r

  • coloring product images in flash/flex approach? 2009-12-02

    I will redesign a product page that contains products like Vase, Ceramic Plates and more, Customers can customize the colors of the product. Now I need some advice what approach would best fit, I am planning to use Flash/Flex/Actionscript in implemen

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.072 (s). 11 q(s)