| You are here: | About>Computing & Technology>C / C++ / C#> Getting Started> What is an Enum? |
![]() | C / C++ / C# |
Suggested ReadingWhat is an Enum?Enums are found in many Programming Languages: Short for enumeration, an enum variable type can be found in C (Ansi, not the original K&R), C++ and C#. The idea is that instead of using an int to represent a set of values, a type with a restricted set of values in used instead. For example if we use the colors of the rainbow, which are
#define red 1
Too many Ints to Count!: The problem with this is that there are many more ints than colors. If violet has the value 7, and the program assigns a value of 15 to a variable then it is clearly a bug but might not be detected as 15 is a valid value for an int.
Enums to the RescueAn enum is a user-defined type consisting of a set of named constants called enumerators. The colors of the rainbow would be mapped like this.:enum rainbowcolors {Now internally, the compiler will use an int to hold these and if no values are supplied, red will be 0, orange is 1 etc. What is the Benefit of an Enum?: The point is that rainbowcolors is a type and only other variables of the same type can be assigned to this. C is easier going (ie less strictly typed), but C++ and C# won't allow assignment unless you force it by using a cast. You aren't stuck with these compiler generated values, you can assign your own integer constant to them as shown here. enum rainbowcolors {Having blue and indigo with the same value isn't a mistake as enumerators might include synonyms such as scarlet and crimson. Language Differences: In C, the variable declaration must be preceded by the word enum as in enum rainbowcolors trafficlights= red;In C++ though, it is not needed as rainbowcolors is a distinct type that doesn't need the enum type prefix. rainbowcolors trafficlights = green;In C# the values are accessed by the type name as in rainbowcolors paint = rainbowcolors.red; What is the Point of Enums?: Using enums increase the level of abstraction and lets the programmer think about what the values mean rather than worry about how they are stored and accessed. This reduces the occurrence of bugs. Here is an example.We have a set of traffic lights with three bulbs- red, yellow and green. In the UK, the sequence of traffic lights changes in these four phases.
Traffic Light Example: The lights are controlled by writing to the bottom three bits of a control byte. These are laid out as a bit pattern below in binary where RYG represent the three bits. If R is 1, the red light is on etc. 00000RYG2In this case, it's easy to see that the four states above correspond to values 4 = Red on, 6= Red + Yellow both on, 1 = Green on and 2 = Yellow on. enum trafficlights { alloff = 0,With this function void SetTrafficLights( trafficlights bulb1,trafficlights bulb 2, int timeon) { Using a Class Instead of Enums: In C++ and C# we'd need to create a class and then overload the operator | to allow oring of types trafficlights. SetTrafficlights( red , yellow, 5) ; // 5 seconds of red and yellow By using enums we prevent problems with other bits being assigned to the bulb control byte. It might be that some of the other bits control self testing or a "Green Lane" switch. In that case a bug that allows these bits to be set in normal use could wreak havoc. To be sure, we'd mask the bits in the SetTrafficlights() function so no matter what value is passed in, only the bottom three bits are changed. Conclusion: Enums have these benefits:
Suggested Reading |
Las Vegas on a BudgetFind a BargainHotel DealsCheap EatsFree AttractionsEntertainment for Less |
All Topics | Email Article | Print this Page | | ![]() |
| Advertising Info | News & Events | Work at About | SiteMap | Reprints | Help | Our Story | Be a Guide |
| User Agreement | Ethics Policy | Patent Info. | Privacy Policy | ©2008 About, Inc., A part of The New York Times Company. All rights reserved. |


