Enumeration Format Strings
You can use the ToString method to create a new string object that represents the numeric, hexadecimal, or string value of an Enum. This method takes one of the enumeration formatting strings to specify the value that you want returned.
The following table lists the enumeration formatting strings and the values they return. These format specifiers are not case-sensitive.
Format string | Result |
---|---|
G or g | Displays the enumeration entry as a string value, if possible, and otherwise displays the integer value of the current instance. If the enumeration is defined with the Flags attribute set, the string values of each valid entry are concatenated together, separated by commas. If the Flags attribute is not set, an invalid value is displayed as a numeric entry. |
F or f | Displays the enumeration entry as a string value, if possible. If the value can be completely displayed as a summation of the entries in the enumeration (even if the Flags attribute is not present), the string values of each valid entry are concatenated together, separated by commas. If the value cannot be completely determined by the enumeration entries, then the value is formatted as the integer value. |
D or d | Displays the enumeration entry as an integer value in the shortest representation possible. |
X or x | Displays the enumeration entry as a hexadecimal value. The value is represented with leading zeros as necessary, to ensure that the value is a minimum eight digits in length. |
The following example defines an enumeration called Colors that consists of three entries: Red, Blue, and Green.
Public Enum Colors
Red = 1
Blue = 2
Green = 3
End Enum
[C#]
public enum Colors{Red = 1, Blue = 2, Green = 3}
After the enumeration is defined, an instance can be declared in the following manner.
Dim MyColors As Colors = Colors.Green
[C#]
Colors MyColors = Colors.Green;
The following example uses the enumeration formatting methods to assign string, numeric, and hexadecimal representations of a DayOfWeek enumeration to the string MyString
. This code creates a new instance of the DayOfWeek enumeration called MyDays
and assigns it the value Friday
. Next, it uses the "G", "F", "D", and "X" formatting strings to assign the different enumeration representations to MyString
.
Dim MyDays As DayOfWeek = DayOfWeek.Friday
Dim MyString As String = MyDays.ToString("G")
' In the U.S. English culture, MyString has the value: "Friday".
MyString = MyDays.ToString("F")
' In the U.S. English culture, MyString has the value: "Friday".
MyString = MyDays.ToString("D")
' In the U.S. English culture, MyString has the value: "5".
MyString = MyDays.ToString("X")
' In the U.S. English culture, MyString has the value: "00000005".
[C#]
DayOfWeek MyDays = DayOfWeek.Friday;
String MyString = MyDays.ToString("G");
// In the U.S. English culture, MyString has the value: "Friday".
MyString = MyDays.ToString("F");
// In the U.S. English culture, MyString has the value: "Friday".
MyString = MyDays.ToString("D");
// In the U.S. English culture, MyString has the value: "5".
MyString = MyDays.ToString("X");
// In the U.S. English culture, MyString has the value: "00000005".