Call me lazy, but i have almost always gone with using static constants where other developers scream enum enum… ENUM!!!
-
public static const TYPE_ONE :String = "typeOne";
-
public static const TYPE_TWO :String = "typeTwo";
-
-
public var type :String = TYPE_ONE;
Ok, so i hear ya, you can chill now, from now one its:
-
package com.vivace.papervision.enum
-
{
-
-
public class SomeEnum
-
{
-
// to ensure enums are not instantiated outside this class we pass a lock class, defined only within this source file (see below)
-
public function SomeEnum(lock:Class, name:String)
-
{
-
super();
-
-
if (lock != ConstructorLock)
-
{
-
throw new Error("Enumerator class can not be instanced directly, use Enum constants");
-
}
-
this.name = name;
-
}
-
-
// [properties] ////////////////////////////////////////////////////////////////////////////////////////////////
-
-
public static const TYPE_ONE :SomeEnum = new SomeEnum(ConstructorLock, "typeOne");
-
public static const TYPE_TWO :SomeEnum = new SomeEnum(ConstructorLock, "typeTwo");
-
-
private var name:String;
-
-
// [public] ////////////////////////////////////////////////////////////////////////////////////////////////////
-
-
public function toString():String
-
{
-
return name;
-
}
-
-
public static function getByName(name:String):SomeEnum
-
{
-
switch(name)
-
{
-
case TYPE_ONE.toString() : return TYPE_ONE; break;
-
case TYPE_TWO.toString() : return TYPE_TWO; break;
-
default : return null;
-
}
-
}
-
-
}
-
}
-
-
// define the lock class outside the package, only accessable internal to this source file
-
class ConstructorLock {}
Improvements, arguments, taking the piss, all welcome.
chur

so whats the deal? how are enum better than static consts? i use consts too and dont see the logic here, or is it just me?
Yo Robbie. Im with you dude… basically the calibre of the developers making the noise was enough to convince me.
The blog entry below, and the resulting comments probably sums up the conversations I have had:
http://blog.jasonnussbaum.com/?p=277
@ robbie
Well lets you have a method that takes 2 params
method( param0:String, param1:String );
Now although there may be a class out there with some constants in it for the params how as a dev do you know what that class is? Also you could ignore those constants and pass through a totally custom invalid String. If Enums are used you would remove these problems
method( param0:Param0Enum, param1:Param1Enum);
Now its clear the first param will only accept properties of type Param0Enum, so immediately as a dev I know what class to use. It would also prevent a dev passing an invalid argument into the method.