enum Statement
Declares the name of an enumerated data type and the names of the members of the enumeration.
[modifiers] enum enumName [ : typeAnnotation]{
enumValue1 [ = initializer1]
[,enumValue2 [ = initializer2]
[, ... [,enumValueN [ = initializerN ] ]]]
}
Arguments
modifiers
Optional. Modifiers that control the visibility and behavior of the enumeration.enumName
Required. Name of the enumerated type.typeAnnotation
Optional. The underlying data type of the enumeration. Must be an integral data type. The default is int.enumValue1, enumValue2, ..., enumValueN
Optional. An enumerated type member.initializer1, initializer2, ..., initializerN
Optional. A constant expression that overrides the default numerical value of an enumeration member.
Remarks
An enum declaration introduces a new enumerated data type into the program. An enum declaration can appear only in contexts where a class declaration can appear, that is, at global scope, at package scope, or at class scope, but not inside a function or method.
You can declare the underlying type of an enumeration to be any integral data type (int, short, long, byte, uint, ushort, ulong, or sbyte). Enumeration members implicitly coerce to and from the underlying data type, allowing for direct assignments of numeric data to variables typed as enum. By default, the underlying data type of an enumeration is int.
Each enumerated type member has a name and an optional initializer. An initializer must be a compile-time, constant expression that is of the same type as the enumeration specified, or convertible to that type. The value of the first enumerated type member is zero or the value of the initializer, if provided. The value of each subsequent enumerated type member is one more then the previous member or the value of the initializer, if provided.
An enum value is accessed in a manner that's similar to accessing a static class member. The name of the member must be qualified with the name of the enumeration, for example Color.Red. When assigning a value to a variable of an enum type, one of following may be used: a fully qualified name (such as Color.Red), a string representation of the name (such as "Red"), or a numeric value.
If an enum is assigned a string that is known at compile time, the compiler will perform the necessary conversion. For example, "Red" would be replaced with Color.Red. If the string is not known at compile time, a conversion will be made at run time. That conversion may fail if the string is not a valid member of the enumerated type. Because the conversion takes time and run-time errors may be generated, avoid assigning an enum to a variable string.
A variable of an enumerated type can hold values outside the range of declared values. One use of this feature is to allow combinations of members used as bit flags, as in done in the example below. Converting an enum variable to a string results in the string representation of the member name.
Example 1
The following example shows the behavior of enumerations. It declares a simple enumeration named CarType that has members Honda, Toyota, and Nissan.
enum CarType {
Honda, // Value of zero, since it is first.
Toyota, // Value of 1, the successor of zero.
Nissan // Value of 2.
}
// Declare a variable of type CarType, and give it the value Honda.
var myCar : CarType = CarType.Honda;
print(int(myCar) + ": " + myCar);
myCar = "Nissan"; // Change the value to "Nissan".
print(int(myCar) + ": " + myCar);
myCar = 1; // 1 is the value of the Toyota member.
print(int(myCar) + ": " + myCar);
The output of this code is:
0: Honda
2: Nissan
1: Toyota
Example 2
The following example shows how to use an enumeration to hold bit flags and also that an enum variable must be able to hold values not explicitly in the member list. It defines an enumeration FormatFlags that is used to modify the behavior of a Format function.
// Explicitly set the type to byte, as there are only a few flags.
enum FormatFlags : byte {
// Can't use the default values, since we need explicit bits
ToUpperCase = 1, // Should not combine ToUpper and ToLower.
ToLowerCase = 2,
TrimLeft = 4, // Trim leading spaces.
TrimRight = 8, // Trim trailing spaces.
UriEncode = 16 // Encode string as a URI.
}
function Format(s : String, flags : FormatFlags) : String {
var ret : String = s;
if(flags & FormatFlags.ToUpperCase) ret = ret.toUpperCase();
if(flags & FormatFlags.ToLowerCase) ret = ret.toLowerCase();
if(flags & FormatFlags.TrimLeft) ret = ret.replace(/^\s+/g, "");
if(flags & FormatFlags.TrimRight) ret = ret.replace(/\s+$/g, "");
if(flags & FormatFlags.UriEncode) ret = encodeURI(ret);
return ret;
}
// Combine two enumeration values and store in a FormatFlags variable.
var trim : FormatFlags = FormatFlags.TrimLeft | FormatFlags.TrimRight;
// Combine two enumeration values and store in a byte variable.
var lowerURI : byte = FormatFlags.UriEncode | FormatFlags.ToLowerCase;
var str : String = " hello, WORLD ";
print(trim + ": " + Format(str, trim));
print(FormatFlags.ToUpperCase + ": " + Format(str, FormatFlags.ToUpperCase));
print(lowerURI + ": " + Format(str, lowerURI));
The output of this code is:
12: hello, WORLD
ToUpperCase: HELLO, WORLD
18: %20%20hello,%20world%20%20