Export (0) Print
Expand All

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)

[ComVisibleAttribute(true)] 
public static Object Parse (
	Type enumType,
	string value
)
/** @attribute ComVisibleAttribute(true) */ 
public static Object Parse (
	Type enumType, 
	String value
)
ComVisibleAttribute(true) 
public static function Parse (
	enumType : Type, 
	value : String
) : Object
Not applicable.

Parameters

enumType

The Type of the enumeration.

value

A string containing the name or value to convert.

Return Value

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

Exception typeCondition

ArgumentNullException

enumType or value is a 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.

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

*/

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

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 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft