Recipe1.16.Displaying an Enumeration Value as a String


Recipe 1.16. Displaying an Enumeration Value as a String

Problem

You need to display the textual or numeric value of an enumeration member.

Solution

To display an enumeration value as a string, use the ToString method that each enumeration member inherits from System.Enum.

Using the following ValidShape enumeration type as an example, you can obtain the textual and numeric values so that you can display them:

 enum ValidShape {     Square, Circle, Cylinder, Octagon } 

Using the ToString method of the ValidShape enumeration type, you can derive the value of a specific ValidShape enumeration value directly:

 Console.WriteLine(ValidShape.Circle.ToString()); Console.WriteLine(ValidShape.Circle.ToString("G")); Console.WriteLine(ValidShape.Circle.ToString("D")); Console.WriteLine(ValidShape.Circle.ToString("F")); Console.WriteLine(ValidShape.Circle.ToString("X")); 

This generates the following output:

 Circle Circle 1 Circle 00000001 

If you are working with a variable of type ValidShape, the enumeration values can be derived in the same manner:

 ValidShape shapeStyle = ValidShape.Cylinder; Console.WriteLine(shapeStyle.ToString()); Console.WriteLine(shapeStyle.ToString("G")); Console.WriteLine(shapeStyle.ToString("D")); Console.WriteLine(shapeStyle.ToString("F")); Console.WriteLine(shapeStyle.ToString("X")); 

The following is displayed.

 Cylinder Cylinder 2 Cylinder 00000002 

Discussion

Deriving the textual or numeric representation of an enumeration value is a simple matter, using the ToString instance method on the Enum type. This method can accept a character indicating the type of formatting to place on the enumeration value. The character can be one of the following: G, g, D, d, X, x, F, or f. See Table 1-2 for a description of these formatting types.

Table 1-2. Formatting types

Formatting type

Name

Description

G or g

(General)

Displays the string representation of the enumeration value.

F or f

(Flag)

Displays the string representation of the enumeration value. The enumeration is treated as if it were a bit field.

D or d

(Decimal)

Displays decimal equivalent of the enumeration.

X or x

(Hexadecimal)

Displays hexadecimal equivalent of the enumeration.


When printing out the values of an enumeration with the Flags attribute, the information displayed takes into account that more than one of the enumeration values may have been ORed together. The output will be all of the enumerations printed out as strings separated by commas or as the ORed numeric value, depending on the formatting chosen. For example, consider if the Flags attribute was added to the ValidShape enumeration as follows:

 [Flags] enum ValidShape {     Square = 0, Circle = 1, Cylinder = 2, Octagon = 4 } 

Now if you change the code for this recipe as follows:

 ValidShape shapeStyle = ValidShape.Circle | ValidShape.Cylinder; Console.WriteLine(shapeStyle.ToString()); Console.WriteLine(shapeStyle.ToString("G")); Console.WriteLine(shapeStyle.ToString("D")); Console.WriteLine(shapeStyle.ToString("F")); Console.WriteLine(shapeStyle.ToString("X")); 

you will see the following output:

 Circle, Cylinder Circle, Cylinder 3 Circle, Cylinder 00000003 

This provides a flexible way of extracting the flags that you are currently using on an enumeration type.

See Also

See the "Enum.ToString Method" and the "Enumeration Format Strings" topics in the MSDN documentation.



C# Cookbook
Secure Programming Cookbook for C and C++: Recipes for Cryptography, Authentication, Input Validation & More
ISBN: 0596003943
EAN: 2147483647
Year: 2004
Pages: 424

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