2/8/2007 204111 204111: Computer and : Computer and Programming Programming : Enumeration Type Enumeration Type Week Week 11 11: Monchai Sopitkamol, Ph.D. Monchai Sopitkamol, Ph.D. Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type � Using an Enumeration Using an Enumeration 1
2/8/2007 Motivations Motivations � Representing some group of symbolic Representing some group of symbolic constants thru integer variables are not constants thru integer variables are not t t t t th th i t i t i bl i bl t t very intuitive. very intuitive. � E.g., representing colors (red, green, blue) E.g., representing colors (red, green, blue) with 0, 1, and 2, respectively. with 0, 1, and 2, respectively. � E g � E.g., representing months (Jan, Feb, Mar, E g E.g., representing months (Jan, Feb, Mar, representing months (Jan Feb Mar representing months (Jan Feb Mar …, Dec) with 0, 1, 2, …, 11, respectively. …, Dec) with 0, 1, 2, …, 11, respectively. Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type � Using an Enumeration Using an Enumeration 2
2/8/2007 Declaring an Enumeration Type Declaring an Enumeration Type enum enum enum enum- -type type { literal literal- -value value1 1 , , literal literal- -value value2 2 , … } , … } E.g., enum Color { Red, Green, Blue } enum Color { Red, Green, Blue } E.g., or enum Month { Jan, Feb, Mar, …, Dec } enum Month { Jan, Feb, Mar, …, Dec } or The symbolic names (literal values) must The symbolic names (literal values) must appear between a pair of braces ({ } ) in a appear between a pair of braces ({ } ) in a comma comma- -separated list. These names separated list. These names enumerate the values of a new type called enumerate the values of a new type called Color Color or or Month Month that is an enumeration type. that is an enumeration type. Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type � Using an Enumeration Using an Enumeration 3
2/8/2007 Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Each enumeration has a set of values associated with Each enumeration has a set of values associated with each element. each element. � By default, the numbering starts at By default, the numbering starts at 0 0 for the first for the first element and goes up in steps of element and goes up in steps of 1 1. . � You can associate a specific integer constant (such as You can associate a specific integer constant (such as 1 1) ) with an enumeration literal (such as Red), as in the with an enumeration literal (such as Red), as in the following example: following example: enum Color { Red = 1 enum Color { Red = 1, Green, Blue } , Green, Blue } � I MPORTANT: I MPORTANT: The integer value that you initialize an The integer value that you initialize an en me ation lite al enumeration literal with must be a compile-time constant enumeration literal with must be a compile en me ation lite al ith m st be a compile ith m st be a compile time constant time constant time constant value (such as value (such as 1 1). That is, it must be a constant whose ). That is, it must be a constant whose value does not depend on any run value does not depend on any run- -time behavior (such time behavior (such as a method call). as a method call). Choosing Enumeration Literal Values Choosing Enumeration Literal Values � If you don't explicitly give an enumeration literal If you don't explicitly give an enumeration literal a constant integer value, the compiler gives it a a constant integer value, the compiler gives it a g g , , p p g g value that is one more than the value of the value that is one more than the value of the previous enumeration literal, except for the very previous enumeration literal, except for the very first enumeration literal, to which the compiler first enumeration literal, to which the compiler gives a value of gives a value of 0 0. . � E.g., in the previous version of E.g., in the previous version of Color Color , the , the underlying values of Red, Green, and Blue are underlying values of Red, Green, and Blue are 1 1, , 2 and 2, and , and 3. and 3 . By default, the declaration “ enum Color { Red, enum Color { Red, � By default, the declaration “ Green, Blue } Green, Blue } ” has the values of Red, Green, has the values of Red, Green, and Blue as and Blue as 0 0, , 1 1, and , and 2 2, respectively. , respectively. 4
2/8/2007 Choosing Enumeration Literal Values Choosing Enumeration Literal Values � You are allowed to give more than one You are allowed to give more than one enumeration literal the same underlying value. enumeration literal the same underlying value enumeration literal the same underlying value enumeration literal the same underlying value. For example, you could code as follows: For example, you could code as follows: “ enum Color { Red, Green, Blue = Red } enum Color { Red, Green, Blue = Red } ” BUT “ enum Color { Red, Green, Blue = White } enum Color { Red, Green, Blue = White } ” will BUT “ will yield compile yield compile- -time error because time error because White White does not exist does not exist in the enumeration! in the enumeration! ALSO “ enum Color { Red = Blue, Green, Blue } enum Color { Red = Blue, Green, Blue } ” will give ALSO “ ” will give compile compile- -time error either because of circular definition time error either because of circular definition error! error! Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type � Using an Enumeration Using an Enumeration 5
2/8/2007 Choosing an Enumeration's Choosing an Enumeration's Underlying Type Underlying Type � When you declare an enumeration type, the When you declare an enumeration type, the enumeration literals are given values of type int enumeration literals are given values of type enumeration literals are given values of type int enumeration literals are given values of type int . int . In other words, the underlying type defaults to In other words, the underlying type defaults to an an int int . . � You can also choose to base your enumeration You can also choose to base your enumeration type on any of the eight integer types: type on any of the eight integer types: byte, byte, sbyte, short, ushort, int, uint, long, sbyte, short, ushort, int, uint, long, or or ulong ulong . � For example, to declare that Color's underlying F F For example, to declare that Color's underlying l l t t d d l l th t C l th t C l ' ' d d l i l i type as a type as a short short rather than an rather than an int int , you can write: , you can write: enum Color : short { Red, Green, Blue } enum Color : short { Red, Green, Blue } Enumeration Type Enumeration Type � Motivations Motivations � Declaring an Enumeration Type Declaring an Enumeration Type � Choosing Enumeration Literal Values Choosing Enumeration Literal Values � Choosing an Enumeration's Underlying Choosing an Enumeration's Underlying Type Type � Using an Enumeration Using an Enumeration 6
2/8/2007 Using an Enumeration Using an Enumeration � Once you have declared your enumeration Once you have declared your enumeration type you can use the name of the type you can use the name of the type, you can use the name of the type, you can use the name of the enumeration as the name of a type in enumeration as the name of a type in exactly the same way as any other type. exactly the same way as any other type. � If If Season Season is the name of your is the name of your enumeration type, you can create enumeration type, you can create variables variables of type variables of type variables of type Season of type Season Season fields Season , , fields fields of type fields of type of type of type Season Season , and , and parameters parameters of type of type Season Season , , as shown in this example: as shown in this example: Using an Enumeration Using an Enumeration enum enum Season Season { { Spring, Spring, Summer, Summer, Fall, Fall, Winter Winter } } class class Example l Example E E l l { { public void public void Method(Season Method(Season parameter parameter) ) { { Season localVariable Season localVariable; ; } } } private private Season Season field field; ; } } 7
Recommend
More recommend