| 1 | = Enumeration = |
| 2 | |
| 3 | An enumeration is a distinct type consisting of a set of named constants (enumerator list, enumeration elements). [[BR]] |
| 4 | By default the underlying type of the enumeration elements is int and the first enumerator |
| 5 | has the value 0 with the value of each successive enumerator increased by 1.[[BR]] |
| 6 | Enumerators can have initializers to override the default values. |
| 7 | |
| 8 | In Cobra an enumeration is declared with the '''enum''' keyword followed by the enumeration name.[[BR]] |
| 9 | As this is a type the name must start with an uppercase letter.[[BR]] |
| 10 | Enumeration element names (with optional initialiser values) follow indented on subsequent lines. |
| 11 | |
| 12 | Emumerations are referenced by the Enumeration class name dotted with the enumeration element name[[BR]] |
| 13 | or like a constructor call: {{{enumName(enumElementName) }}}. |
| 14 | |
| 15 | Multiple elements can be or'd together by specifying multiple elementNames comma separated |
| 16 | within the constructor-like call. |
| 17 | |
| 18 | == Grammar == |
| 19 | {{{ |
| 20 | enum <enumName> [ is <accessModifier>] |
| 21 | [ has <Attributes>] |
| 22 | [ of <Type> ] |
| 23 | [ <DocString> ] |
| 24 | <enumItemName> [ = <value> ] |
| 25 | ... |
| 26 | |
| 27 | e = <enumName>.<enumItemName> |
| 28 | e1 = <enumName>(<enumItemName>[, <enumItemName> ...]) |
| 29 | }}} |
| 30 | |
| 31 | == Platform == |
| 32 | |
| 33 | == Examples == |
| 34 | {{{ |
| 35 | enum Days |
| 36 | """In this enumeration, Sat is 0, Sun is 1, Mon is 2, and so forth.""" |
| 37 | Sat |
| 38 | Sun |
| 39 | Mon |
| 40 | Tue |
| 41 | Wed |
| 42 | Thu |
| 43 | Fri |
| 44 | |
| 45 | class UseDays |
| 46 | def main is shared |
| 47 | today = Days.Fri |
| 48 | yesterday = Day(Thu) |
| 49 | weekend = Days(Sat, Sun) |
| 50 | }}} |
| 51 | |
| 52 | See the effect of System.!FlagsAttribute on an enum |
| 53 | {{{ |
| 54 | enum CarOptions |
| 55 | has Flags |
| 56 | SunRoof = 0x01 |
| 57 | Spoiler = 0x02 |
| 58 | FogLights = 0x04 |
| 59 | TintedWindows = 0x08 |
| 60 | |
| 61 | class FlagTest |
| 62 | def main is shared |
| 63 | options = CarOptions(SunRoof,FogLights) |
| 64 | print options |
| 65 | print options to int |
| 66 | |
| 67 | /# Output is |
| 68 | SunRoof, FogLights |
| 69 | 5 |
| 70 | Without 'has Flags' on enum CarOptions |
| 71 | 5 |
| 72 | 5 |
| 73 | #/ |
| 74 | }}} |
| 75 | |
| 76 | == Notes == |
| 77 | It is suggested all enumerations have an element whose value is 0.[[BR]] |
| 78 | If this is in addition to the default starting point of values it should be named '''None'''. |
| 79 | |
| 80 | == See Also == |
| 81 | |
| 82 | [http://msdn.microsoft.com/en-us/library/sbbt4032%28VS.80%29.aspx enum(C# Reference)] |
| 83 | |
| 84 | |
| 85 | [wiki:LanguageTopics Back to LanguageTopics] |