This documentation is archived and is not being maintained.

Enum.Parse Method

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

Overload List

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

[Visual Basic] Overloads Public Shared Function Parse(Type, String) As Object
[C#] public static object Parse(Type, string);
[C++] public: static Object* Parse(Type*, String*);
[JScript] public static function Parse(Type, String) : Object;

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-sensitive.

[Visual Basic] Overloads Public Shared Function Parse(Type, String, Boolean) As Object
[C#] public static object Parse(Type, string, bool);
[C++] public: static Object* Parse(Type*, String*, bool);
[JScript] public static function Parse(Type, String, Boolean) : Object;

Example

The following code sample 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 'Colors

    Public Shared Sub Main()
        
        Console.WriteLine("The entries of the Colors Enum are:")
        Dim s As String
        For Each s In  [Enum].GetNames(GetType(Colors))
            Console.WriteLine(s)
        Next s

        Console.WriteLine()

        Dim myOrange As Colors = CType([Enum].Parse(GetType(Colors), "Red, Yellow"), Colors)
        
        Console.WriteLine("The myOrange value has the combined entries of {0}", myOrange)
    End Sub 'Main
End Class 'ParseTest

[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 s in Enum.GetNames(typeof(Colors)))
            Console.WriteLine(s);

        Console.WriteLine();

        Colors myOrange = (Colors)Enum.Parse(typeof(Colors), "Red, Yellow");
        Console.WriteLine("The myOrange value has the combined entries of {0}", myOrange);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;

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

int main() {

    Console::WriteLine("The entries of the Colors Enum are:");
    
    Array* a = Enum::GetNames(__typeof(Colors));
    Int32 i = 0;

    while(i < a->Length){
        Object* o = a->GetValue(i);
        Console::WriteLine(o->ToString());
        i++;
    }

    Console::WriteLine();

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

[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);
    }
}

See Also

Enum Class | Enum Members | System Namespace

Show: