Enumeration Format Strings
You can use the Enum.ToString method to create a new string object that represents the numeric, hexadecimal, or string value of an enumeration member. 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.
Example
The following example defines an enumeration called Colors that consists of three entries: Red, Blue, and Green.
Public Enum Color
Red = 1
Blue = 2
Green = 3
End Enum
public enum Color {Red = 1, Blue = 2, Green = 3}
After the enumeration is defined, an instance can be declared in the following manner.
Dim myColor As Color = Color.Green
Color myColor = Color.Green;
The Color.ToString(System.String) method can then be used to display the enumeration value in different ways, depending on the format specifier passed to it.
Console.WriteLine("The value of myColor is {0}.", _
myColor.ToString("G"))
Console.WriteLine("The value of myColor is {0}.", _
myColor.ToString("F"))
Console.WriteLine("The value of myColor is {0}.", _
myColor.ToString("D"))
Console.WriteLine("The value of myColor is 0x{0}.", _
myColor.ToString("X"))
' The example displays the following output to the console:
' The value of myColor is Green.
' The value of myColor is Green.
' The value of myColor is 3.
' The value of myColor is 0x00000003.
Console.WriteLine("The value of myColor is {0}.",
myColor.ToString("G"));
Console.WriteLine("The value of myColor is {0}.",
myColor.ToString("F"));
Console.WriteLine("The value of myColor is {0}.",
myColor.ToString("D"));
Console.WriteLine("The value of myColor is 0x{0}.",
myColor.ToString("X"));
// The example displays the following output to the console:
// The value of myColor is Green.
// The value of myColor is Green.
// The value of myColor is 3.
// The value of myColor is 0x00000003.