Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
Enum Class
Enum Methods
Parse Method
 Parse Method (Type, String)
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
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)
Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
Public Shared Function Parse ( _
    enumType As Type, _
    value As String _
) As Object
Visual Basic (Usage)
Dim enumType As Type
Dim value As String
Dim returnValue As Object

returnValue = Enum.Parse(enumType, _
    value)
C#
[ComVisibleAttribute(true)]
public static Object Parse(
    Type enumType,
    string value
)
Visual C++
[ComVisibleAttribute(true)]
public:
static Object^ Parse(
    Type^ enumType, 
    String^ value
)
JScript
public static function Parse(
    enumType : Type, 
    value : String
) : Object

Parameters

enumType
Type: System..::.Type
The Type of the enumeration.
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.
ExceptionCondition
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.

The value parameter contains a value, a named constant, or a list of named constants delimited by commas (,). One or more blanks 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.

This operation is case-sensitive.

The following example illustrates the use of Parse, taking a Type and a String as parameters.

Visual Basic
Imports System

Public Class ParseTest

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

    Public Shared Sub Main()
        Console.WriteLine("The entries of the Colors Enum are:")
        Dim colorName As String
        For Each colorName In [Enum].GetNames(GetType(Colors))
            Console.WriteLine("{0}={1}", colorName, Convert.ToInt32([Enum].Parse(GetType(Colors), colorName)))
        Next colorName
        Console.WriteLine()
        Dim myOrange As Colors = CType([Enum].Parse(GetType(Colors), "Red, Yellow"), Colors)
        Console.WriteLine("The myOrange value {1} has the combined entries of {0}", myOrange, Convert.ToInt64(myOrange))
    End Sub
End Class

'This code example produces the following results:
'
'The entries of the Colors Enum are:
'Red=1
'Green=2
'Blue=4
'Yellow=8
'
'The myOrange value 9 has the combined entries of Red, Yellow
'

C#
using System;

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

    public static void Main()
    {
        Console.WriteLine("The entries of the Colors Enum are:");
        foreach (string colorName in Enum.GetNames(typeof(Colors)))
        {
            Console.WriteLine("{0}={1}", colorName, 
                                         Convert.ToInt32(Enum.Parse(typeof(Colors), colorName)));
        }
        Console.WriteLine();
        Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
        Console.WriteLine("The myOrange value {1} has the combined entries of {0}", 
                           myOrange, Convert.ToInt64(myOrange));
    }
}

/*
This code example produces the following results:

The entries of the Colors Enum are:
Red=1
Green=2
Blue=4
Yellow=8

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

*/

Visual C++
using namespace System;

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

int main()
{
   Console::WriteLine(  "The entries of the Colors Enum 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^ myOrange = Enum::Parse( Colors::typeid,  "Red, Yellow" );
   Console::WriteLine(  "The myOrange value has the combined entries of {0}", myOrange );
}

/*
This code example produces the following results:

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

The myOrange value has the combined entries of Red, Yellow

*/

J#
import System.*;

public class ParseTest
{
    /** @attribute FlagsAttribute()
     */
    enum Colors
    {
        red  (1),
        green  (2),
        blue  (4),
        yellow  (8);
    } //Colors

    public static void main(String[] args)
    {
        Console.WriteLine("The entries of the Colors Enum are:");

        String s[] = Enum.GetNames(Colors.class.ToType());
        for (int iCtr = 0; iCtr < s.length; iCtr++) {
            Console.WriteLine(s[iCtr]);
        }
        Console.WriteLine();

        Colors myOrange = ((Colors)(Enum.Parse(Colors.class.ToType(), 
            "red, yellow")));
        Console.WriteLine("The myOrange value has the combined entries of {0}",
            myOrange);
    } //main
} //ParseTest

JScript
import System;

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

    public static function Main() {
        Console.WriteLine("The entries of the Colors Enum are:");
        for(var i : int in Enum.GetNames(Colors))
            Console.WriteLine(Enum.GetNames(Colors).GetValue(i));
        Console.WriteLine();
        var myOrange : Colors = Colors(Enum.Parse(Colors, "Red, Yellow"));
        Console.WriteLine("The myOrange value has the combined entries of {0}", myOrange);
    }
}

ParseTest.Main();

/*
This code example produces the following results:

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

The myOrange value has the combined entries of Red, Yellow

*/

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker