Enum.Parse Method (Type, String, Boolean)

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

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. A parameter specifies whether the operation is case-insensitive.

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

Syntax

Public Shared Function Parse ( _
    enumType As Type, _
    value As String, _
    ignoreCase As Boolean _
) As Object
public static Object Parse(
    Type enumType,
    string value,
    bool ignoreCase
)

Parameters

Return Value

Type: System..::.Object
An object of type enumType whose value is represented by value.

Exceptions

Exception Condition
ArgumentNullException

enumType or value is nullNothingnullptra null reference (Nothing in Visual Basic).

ArgumentException

enumType is not an Enum.

-or-

value is either an empty string ("") or only contains white space.

-or-

value is a name, but not one of the named constants defined for the enumeration.

OverflowException

value is outside the range of the underlying type of enumType.

Remarks

The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants delimited by commas (,). One or more blank spaces can precede or follow each value, name, or comma in value. If value is a list, the return value is the value of the specified names combined with a bitwise OR operation.

If value is a name that does not correspond to a named constant of enumType, the method throws an ArgumentException. If value is the string representation of an integer that does not represent an underlying value of the enumType enumeration, the method returns an enumeration member whose underlying value is value converted to an integral type. If this behavior is undesirable, call the IsDefined method to ensure that a particular string representation of an integer is actually a member of enumType. The following example defines a Colors enumeration, calls the Parse(Type, String, Boolean) method to convert strings to their corresponding enumeration values, and calls the IsDefined method to ensure that particular integral values are underlying values in the Colors enumeration.

<Flags()> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then
               outputBlock.Text += String.Format("Converted '{0}' to {1}.", colorString, colorValue.ToString()) & vbCrLf
            Else
               outputBlock.Text += String.Format("{0} is not an underlying value of the Colors enumeration.", colorString) & vbCrLf
            End If
         Catch e As ArgumentException
            outputBlock.Text += String.Format("{0} is not a member of the Colors enumeration.", colorString) & vbCrLf
         End Try
      Next
   End Sub
End Module
' The example displays the following output:
'       Converted '0' to None.
'       Converted '2' to Green.
'       8 is not an underlying value of the Colors enumeration.
'       Converted 'blue' to Blue.
'       Converted 'Blue' to Blue.
'       Yellow is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.
using System;

[Flags]
enum Colors { None = 0, Red = 1, Green = 2, Blue = 4 };

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try
         {
            Colors colorValue = (Colors)Enum.Parse(typeof(Colors), colorString, true);
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))
               outputBlock.Text += String.Format("Converted '{0}' to {1}.", colorString, colorValue.ToString()) + "\n";
            else
               outputBlock.Text += String.Format("{0} is not an underlying value of the Colors enumeration.", colorString) + "\n";
         }
         catch (ArgumentException)
         {
            outputBlock.Text += String.Format("{0} is not a member of the Colors enumeration.", colorString) + "\n";
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.

The ignoreCase parameter specifies whether this operation is case-sensitive.

Examples

The following example uses the Parse(Type, String, Boolean) method to parse the string representation of an enumeration value that consists of a bit field.


Public Class Example

   <FlagsAttribute()> _
   Enum Colors
      Red = 1
      Green = 2
      Blue = 4
      Yellow = 8
   End Enum

   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim myOrange As Colors = CType([Enum].Parse(GetType(Colors), "Red, Yellow", True), Colors)
      outputBlock.Text += String.Format("The myOrange value {1} has the combined entries of {0}", myOrange, Convert.ToInt64(myOrange)) & vbCrLf
   End Sub
End Class

'This code example produces the following results:
'
'The myOrange value 9 has the combined entries of Red, Yellow
'
using System;

public class Example
{
   [FlagsAttribute]
   enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 };

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow", true);
      outputBlock.Text += String.Format("The myOrange value {1} has the combined entries of {0}",
                         myOrange, Convert.ToInt64(myOrange)) + "\n";
   }
}

/*
This code example produces the following results:

    The myOrange value 9 has the combined entries of Red, Yellow

*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

Enum Class

Parse Overload

System Namespace

IsDefined

TryParse<(Of <(TEnum>)>)