ToString Method (String)
Collapse the table of content
Expand the table of content

Enum.ToString Method (String)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

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

Parameters

format
Type: System.String
A format string.

Return Value

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

ExceptionCondition
FormatException

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
      Red
      Green
      Blue
      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
'


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2017 Microsoft