Freigeben über


Enum.TryParse<TEnum> Method (String, TEnum%)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. The return value indicates whether the conversion succeeded.

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

Syntax

'Declaration
Public Shared Function TryParse(Of TEnum As {Structure, New}) ( _
    value As String, _
    <OutAttribute> ByRef result As TEnum _
) As Boolean
public static bool TryParse<TEnum>(
    string value,
    out TEnum result
)
where TEnum : struct, new()

Type Parameters

  • TEnum
    The enumeration type to which to convert value.

Parameters

  • value
    Type: System.String
    The string representation of the enumeration name or underlying value to convert.
  • result
    Type: TEnum%
    When this method returns, contains an object of type TEnum whose value is represented by value. This parameter is passed uninitialized.

Return Value

Type: System.Boolean
true if the value parameter was converted successfully; otherwise, false.

Exceptions

Exception Condition
ArgumentException

TEnum is not an enumeration type.

Remarks

TryParse<TEnum>(String, TEnum%) is similar to the Parse(Type, String, Boolean) method, except that instead of throwing an exception, it returns false if the conversion fails. It eliminates the need for exception handling when parsing the string representation of an enumeration value.

The value parameter contains the string representation of an enumeration member's underlying value or named constant, or a list of named constants or underlying values delimited by commas (,). If value includes multiple named constants or values, one or more blank spaces can precede or follow each value, name, or comma in value. If value is a list, result reflects the value of the specified names or underlying values combined with a bitwise OR operation. If value is the string representation of the name of an enumeration value, the comparison of value with enumeration names is case-sensitive.

If value is a name that does not correspond to a named constant of TEnum, the method returns false. If value is the string representation of an integer that does not represent an underlying value of the TEnum 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 TEnum.

Examples

The following example defines a Colors enumeration, calls the TryParse<TEnum>(String, TEnum%) 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
         Dim colorValue As Colors
         If [Enum].TryParse(colorString, colorValue) Then
            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
         Else
            outputBlock.Text += String.Format("{0} is not a member of the Colors enumeration.", colorString) & vbCrLf
         End If
      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.
'    blue is not a member of the Colors enumeration.
'    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)
      {
         Colors colorValue;
         if (Enum.TryParse(colorString, out colorValue))
            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";
         else
            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.
//       blue is not a member of the Colors enumeration.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.

Version Information

Silverlight

Supported in: 5, 4

Platforms

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