Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Enum::Parse Method (Type^, String^)

 

Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

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

public:
[ComVisibleAttribute(true)]
static Object^ Parse(
	Type^ enumType,
	String^ value
)

Parameters

enumType
Type: System::Type^

An enumeration type.

value
Type: System::String^

A string containing the name or value to convert.

Return Value

Type: System::Object^

An object of type enumType whose value is represented by value.

Exception Condition
ArgumentNullException

enumType or value is null.

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.

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^) 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.

No code example is currently available or this language may not be supported.

This operation is case-sensitive.

The following example uses the Parse(Type^, String^) method to parse an array of strings that are created by calling the GetNames method. It also uses the Parse(Type^, String^) method to parse an enumeration value that consists of a bit field.

using namespace System;

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

int main()
{
   Console::WriteLine(  "The entries of the Colors enumeration are:" );
   Array^ a = Enum::GetNames( Colors::typeid );
   Int32 i = 0;
   while ( i < a->Length )
   {
      Object^ o = a->GetValue( i );
      Console::WriteLine( o->ToString() );
      i++;
   }

   Console::WriteLine();
   Object^ orange = Enum::Parse( Colors::typeid,  "Red, Yellow" );
   Console::WriteLine("The orange value has the combined entries of {0}", orange );
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red
Green
Blue
Yellow

The orange value has the combined entries of Red, Yellow

*/

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: