The following example shows the behavior of enumerations
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
// 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
Numan
--------------------------------------------------
Love is the most precious thing of this world. So find it and grab it!
|