Monday, December 30, 2019
What Is an Enum in Programming Languages
Short for enumeration, an enum variable type can be found in C (ANSI, not the original KR), 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 is used instead. For example, if we use the colors of the rainbow, which are RedOrangeYellowGreenBlueIndigoViolet If enums didnt exist, you might use a #define (in C) or const in C/C# to specify these values. Eg 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 Rescue An 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.: 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# wont allow assignment unless you force it by using a cast. You arent stuck with these compiler generated values, you can assign your own integer constant to them as shown here. Having blue and indigo with the same value isnt 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 In C though, it is not needed as rainbowcolors is a distinct type that doesnt need the enum type prefix. In C# the values are accessed by the type name as in 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. Red - Traffic Stopped.Both Red and Yellow - Traffic Still stopped, but lights about to change to green.Green - Traffic can move.Yellow - Warning of imminent change to red. 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. In this case, its 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. With this function Using a Class Instead of Enums In C and C# wed need to create a class and then overload the operator | to allow OR-ing of types trafficlights. 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, wed 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: They restrict the values that the enum variable can take.They force you to think about all the possible values that the enum can take.They are a constant rather than a number, increasing readability of the source code
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.