Wiki

Changes between Initial Version and Version 1 of Enum

Show
Ignore:
Timestamp:
05/14/10 14:26:00 (15 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Enum

    v1 v1  
     1= Enumeration =  
     2 
     3An enumeration is a distinct type consisting of a set of named constants (enumerator list, enumeration elements). [[BR]] 
     4By default the underlying type of the enumeration elements is int and the first enumerator 
     5has the value 0 with the value of each successive enumerator increased by 1.[[BR]]  
     6Enumerators can have initializers to override the default values. 
     7 
     8In Cobra an enumeration is declared with the '''enum''' keyword followed by the enumeration name.[[BR]] 
     9As this is a type the name must start with an uppercase letter.[[BR]] 
     10Enumeration element names (with optional initialiser values) follow indented on subsequent lines. 
     11 
     12Emumerations are referenced by the Enumeration class name dotted with the enumeration element name[[BR]] 
     13or like a constructor call: {{{enumName(enumElementName) }}}. 
     14 
     15Multiple elements can be or'd together by specifying multiple elementNames comma separated  
     16within the constructor-like call. 
     17 
     18== Grammar == 
     19{{{ 
     20enum <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{{{ 
     35enum 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 
     45class UseDays 
     46    def main is shared 
     47        today = Days.Fri 
     48        yesterday = Day(Thu) 
     49        weekend = Days(Sat, Sun) 
     50}}} 
     51 
     52See the effect of System.!FlagsAttribute on an enum  
     53{{{ 
     54enum CarOptions  
     55    has Flags 
     56    SunRoof = 0x01 
     57    Spoiler = 0x02 
     58    FogLights = 0x04 
     59    TintedWindows = 0x08 
     60 
     61class 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 == 
     77It is suggested all enumerations have an element whose value is 0.[[BR]]  
     78If 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]