Grouping Constants into Enumerated Types


Enumerated types are a set of numeric constants related to one another. Suppose you wanted to represent the days of the week numerically . With enumerated types, you could declare constants like Sunday = 1, Monday = 2, Tuesday = 3, Wednesday = 4, Thursday = 5, Friday = 6, Saturday = 7 into one type called DaysOfTheWeek. Then you could use DaysOfTheWeek as a type for declaring variables .

To declare an enumerated type:

  1. Type the keyword enum .

  2. Type the name of the enumerated type, DaysOfTheWeek , for example, or KindsOfFood .

  3. Type an open curly bracket {.

  4. Type the name of each constant in the group separated by commas. You can assign each constant a value. If you don't assign a value, the compiler will assign sequential values starting at zero.

  5. Type a close curly bracket } ( Figure 2.23 ).

    Figure 2.23 Enumerated types enable you to group several related constants.
     enum DaysOfTheWeek {    None=0,    Sunday=1,    Monday=2,    Tuesday=3,    Wednesday=4,    Thursday=5,    Friday=6,    Saturday=7 } 

graphics/tick.gif Tips

  • Unlike variables and constants that can only be declared inside of a class or inside of a function, enumerated types are stand-alone types, and as such can be declared either outside or inside a class. They can't be declared inside of functions.

  • You can use enumerated types in variable declarations ( Figure 2.24 ). When you use the enumerated type in a variable declaration, the compiler forces the developer to use one of the constants in the enum rather than a random value. This makes the code more readable and less error prone ( Figure 2.25 ).

    Figure 2.24 Enums become types. As types they can be used in variable declarations.
      DaysOfTheWeek  apptDay; 
    Figure 2.25 If your variable is of an enum type the compiler will only let you assign constants in the enum to the variable. You can't just use numbers .
     //use the name of the enum plus the constant name. DaysOfTheWeek apptday =  DaysOfTheWeek.Tuesday  ; //*** illegal *** DaysOfTheWeek today = 15; 
  • If you wish to assign to an enumerated type variable a value (rather than a constant name) you must use a procedure called casting. Casting means that you put the name of the enumerated type in parenthesis in front of the numeric value ( Figure 2.26 ). Be careful with this technique. The compiler won't stop you from assigning a value outside of the range of the enum.

    Figure 2.26 If you want to use the numeric value, you have to use a mechanism called casting. Casting converts the number into the constant in the enum.
     DaysOfTheWeek apptDay =  (DaysOfTheWeek)7  ; //be careful, the compiler will not stop //an incorrect value DaysOfTheWeek apptDay =  (DaysOfTheWeek)15  ; 
  • Enum constants can hold integer values by default. You can change the type of the constants to byte, short or long (actually the complete list includes: byte, sbyte, short, ushort, int, uint, long, or ulong). The type of the constant is changed by putting a colon followed by the type after the name of the enumerated type ( Figure 2.27 ).

    Figure 2.27 Enums are integers by default, but if you're only going to need a short range of values you can change the type to something smaller, or bigger if you need a greater range.
     enum DaysOfTheWeek  : byte  {    None,    Sunday,    Monday,    Tuesday,    Wednesday,    Thursday,    Friday,    Saturday } 
  • Enum constants aren't meant to be combined by default. For example in a variable declaration like DaysOfTheWeek today , today is supposed to hold a single value from the DaysOfTheWeek constants. However, imagine an enumerated type called EmailFormats and a variable declaration like EmailFormats userPreferences . It is possible that the user may want to combine some of the values. If the values are meant to be combined, two things need to happen. First, you have to add the attribute [Flags] in front of the enum, and second, you have to number the constants using a power of 2 (1, 2, 4, 8, 16, 32, etc.) ( Figure 2.28 ).

    Figure 2.28 The and & operators are used to do bit arithmetic. The operator is used to take the union of two values, while & is used to take the intersection.
      [Flags]  enum EmailFormats {    plainText=1,    HTML=2,    Attachments=4 } //constants are combined with // the  symbol  EmailFormats userPreferences =   EmailFormats.plainText   EmailFormats.HTML   EmailFormats.Attachments;  //you can test for a value in the // variable using the & symbol bool allowAttachments = (  userPreferences &   EmailFormats.Attachments  ) != 0; 



C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net