Enum.ToString Method (String)

Updated: August 2009

Converts the value of this instance to its equivalent string representation using the specified format.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Public Function ToString ( _
	format As String _
) As String


Type: System.String
A format string.

Return Value

Type: System.String
The string representation of the value of this instance as specified by format.


format contains an invalid specification.

The format parameter can contain the "G" or "g", "D" or "d", "X" or "x", and "F" or "f" format strings. If format is Nothing or an empty string (""), the general format specifier ("G") is used. For more information about formatting enumeration values, see Enumeration Format Strings. For more information about formatting in general, see Formatting Types.

Notes to Callers

If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value.

Public Enum Shade
   White = 0
   Gray = 1
   Grey = 1
   Black = 2
End Enum

The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned.

Dim shadeName As String = CType(1, Shade).ToString("F")

The following example demonstrates how to convert an enumerated value to a string.

' Sample for Enum.ToString(String)

Class Example
   Enum Colors
      Yellow = 12
   End Enum 'Colors

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myColor As Colors = Colors.Yellow

      outputBlock.Text &= String.Format("Colors.Red = {0}", Colors.Red.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("Colors.Green = {0}", Colors.Green.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("Colors.Blue = {0}", Colors.Blue.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("Colors.Yellow = {0}", Colors.Yellow.ToString("d")) & vbCrLf

      outputBlock.Text &= String.Format("{0}myColor = Colors.Yellow{0}", vbCrLf) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""g"") = {0}", myColor.ToString("g")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""G"") = {0}", myColor.ToString("G")) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""x"") = {0}", myColor.ToString("x")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""X"") = {0}", myColor.ToString("X")) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""d"") = {0}", myColor.ToString("d")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""D"") = {0}", myColor.ToString("D")) & vbCrLf

      outputBlock.Text &= String.Format("myColor.ToString(""f"") = {0}", myColor.ToString("f")) & vbCrLf
      outputBlock.Text &= String.Format("myColor.ToString(""F"") = {0}", myColor.ToString("F")) & vbCrLf
   End Sub 'Main
End Class 'Sample
'This example produces the following results:
'Colors.Red = 0
'Colors.Green = 1
'Colors.Blue = 2
'Colors.Yellow = 12
'myColor = Colors.Yellow
'myColor.ToString("g") = Yellow
'myColor.ToString("G") = Yellow
'myColor.ToString("x") = 0000000C
'myColor.ToString("X") = 0000000C
'myColor.ToString("d") = 12
'myColor.ToString("D") = 12
'myColor.ToString("f") = Yellow
'myColor.ToString("F") = Yellow


Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.




August 2009

Added the Notes for Callers section.

Customer feedback.

Community Additions