Enum.Parse Enum.Parse Enum.Parse Enum.Parse Method

定義

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

オーバーロード

Parse(Type, String) Parse(Type, String) Parse(Type, String) Parse(Type, String)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

Parse(Type, String, Boolean) Parse(Type, String, Boolean) Parse(Type, String, Boolean) Parse(Type, String, Boolean)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。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-insensitive.

Parse<TEnum>(String, Boolean) Parse<TEnum>(String, Boolean) Parse<TEnum>(String, Boolean) Parse<TEnum>(String, Boolean)
Parse<TEnum>(String) Parse<TEnum>(String) Parse<TEnum>(String) Parse<TEnum>(String)

Parse(Type, String) Parse(Type, String) Parse(Type, String) Parse(Type, String)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.

public:
 static System::Object ^ Parse(Type ^ enumType, System::String ^ value);
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse (Type enumType, string value);
static member Parse : Type * string -> obj
Public Shared Function Parse (enumType As Type, value As String) As Object

パラメーター

enumType
Type Type Type Type

列挙型。An enumeration type.

value
String String String String

変換する名前または値が含まれている文字列。A string containing the name or value to convert.

戻り値

value によって表される値を持つ enumType 型のオブジェクト。An object of type enumType whose value is represented by value.

例外

enumType または valuenull です。enumType or value is null.

enumTypeEnum ではありません。enumType is not an Enum.

- または --or- value が空の文字列であるか、または空白しか含まれていません。value is either an empty string or only contains white space.

- または --or- value は名前ですが、列挙に対して定義されている名前付き定数の名前ではありません。value is a name, but not one of the named constants defined for the enumeration.

valueenumType の基になる型の範囲外です。value is outside the range of the underlying type of enumType.

次の例では、Parse(Type, String)メソッドを呼び出すことによって作成される文字列の配列を解析、GetNamesメソッド。The following example uses the Parse(Type, String) method to parse an array of strings that are created by calling the GetNames method. また、使用、Parse(Type, String)ビット フィールドで構成される列挙値を解析します。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

*/
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 enumeration are:");
        foreach (string colorName in Enum.GetNames(typeof(Colors)))
        {
            Console.WriteLine("{0} = {1:D}", colorName, 
                                         Enum.Parse(typeof(Colors), colorName));
        }
        Console.WriteLine();

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

/*
This code example produces the following results:

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

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

*/
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 enumeration are:")
        Dim colorName As String
        For Each colorName In [Enum].GetNames(GetType(Colors))
            Console.WriteLine("{0} = {1:D}", colorName, [Enum].Parse(GetType(Colors), colorName))
        Next
        Console.WriteLine()

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

'This example displays the following output:
'
'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
'

注釈

valueパラメーターには、列挙体のメンバーの基になる値または名前付き定数の文字列表現が含まれているかの一覧に名前付き定数のコンマ (,) で区切られます。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 (,). 1 つ以上の空白文字が前または次の各値、名、またはコンマでvalueします。One or more blank spaces can precede or follow each value, name, or comma in value. 場合valueリストで、戻り値は、指定した名前の演算と組み合わせる値OR操作。If value is a list, the return value is the value of the specified names combined with a bitwise OR operation.

場合valueの名前付き定数に対応していない名前は、enumTypeがスローされます、ArgumentExceptionします。If value is a name that does not correspond to a named constant of enumType, the method throws an ArgumentException. 場合valueの基になる値を表していない整数の文字列表現です、enumType列挙型、メソッドは基になる値が列挙体のメンバーを返しますvalue整数型に変換します。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. この動作が望ましくない場合は、呼び出し、 IsDefined 、整数の特定の文字列表現が実際のメンバーであることを確認するメソッドenumTypeします。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. 次の例では、定義、Colors列挙型、呼び出し、Parse(Type, String)メソッドを呼び出し、対応する列挙値を文字列に変換する、IsDefinedで値を特定の整数値が基になることを確認する方法Colors列挙体。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.

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };
   
public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString);        
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))  
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// 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.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("'{0}' is not a member of the Colors enumeration.", colorString)
         End Try
      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.

この操作は、大文字小文字を区別します。This operation is case-sensitive.

こちらもご覧ください

Parse(Type, String, Boolean) Parse(Type, String, Boolean) Parse(Type, String, Boolean) Parse(Type, String, Boolean)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。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-insensitive.

public:
 static System::Object ^ Parse(Type ^ enumType, System::String ^ value, bool ignoreCase);
[System.Runtime.InteropServices.ComVisible(true)]
public static object Parse (Type enumType, string value, bool ignoreCase);
static member Parse : Type * string * bool -> obj
Public Shared Function Parse (enumType As Type, value As String, ignoreCase As Boolean) As Object

パラメーター

enumType
Type Type Type Type

列挙型。An enumeration type.

value
String String String String

変換する名前または値が含まれている文字列。A string containing the name or value to convert.

ignoreCase
Boolean Boolean Boolean Boolean

大文字と小文字を区別しない場合は true。大文字と小文字を区別する場合は falsetrue to ignore case; false to regard case.

戻り値

value によって表される値を持つ enumType 型のオブジェクト。An object of type enumType whose value is represented by value.

例外

enumType または valuenull です。enumType or value is null.

enumTypeEnum ではありません。enumType is not an Enum.

- または --or- value が空の文字列 ("") であるか、または空白しか含まれていません。value is either an empty string ("") or only contains white space.

- または --or- value は名前ですが、列挙に対して定義されている名前付き定数の名前ではありません。value is a name, but not one of the named constants defined for the enumeration.

valueenumType の基になる型の範囲外です。value is outside the range of the underlying type of enumType.

次の例では、Parse(Type, String, Boolean)メソッドを呼び出すことによって作成される文字列の配列を解析、GetNamesメソッド。The following example uses the Parse(Type, String, Boolean) method to parse an array of strings that are created by calling the GetNames method. また、使用、Parse(Type, String)ビット フィールドで構成される列挙値を解析します。It also uses the Parse(Type, String) method to parse an enumeration value that consists of a bit field.

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };
   
public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString, true);        
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))  
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End Try
      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.
'       Converted 'blue' to Blue.
'       Converted 'Blue' to Blue.
'       Yellow is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.

注釈

valueパラメーターには、列挙体のメンバーの基になる値または名前付き定数の文字列表現が含まれているかの一覧に名前付き定数のコンマ (,) で区切られます。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 (,). 1 つ以上の空白文字が前または次の各値、名、またはコンマでvalueします。One or more blank spaces can precede or follow each value, name, or comma in value. 場合valueリストで、戻り値は、指定した名前の演算と組み合わせる値OR操作。If value is a list, the return value is the value of the specified names combined with a bitwise OR operation.

場合valueの名前付き定数に対応していない名前は、enumTypeがスローされます、ArgumentExceptionします。If value is a name that does not correspond to a named constant of enumType, the method throws an ArgumentException. 場合valueの基になる値を表していない整数の文字列表現です、enumType列挙型、メソッドは基になる値が列挙体のメンバーを返しますvalue整数型に変換します。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. この動作が望ましくない場合は、呼び出し、 IsDefined 、整数の特定の文字列表現が実際のメンバーであることを確認するメソッドenumTypeします。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. 次の例では、定義、Colors列挙型、呼び出し、Parse(Type, String, Boolean)メソッドを呼び出し、対応する列挙値を文字列に変換する、IsDefinedで値を特定の整数値が基になることを確認する方法Colors列挙体。The following example defines a Colors enumeration, calls the Parse(Type, String, Boolean) 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.

using System;

[Flags] enum Colors { None=0, Red = 1, Green = 2, Blue = 4 };
   
public class Example
{
   public static void Main()
   {
      string[] colorStrings = { "0", "2", "8", "blue", "Blue", "Yellow", "Red, Green" };
      foreach (string colorString in colorStrings)
      {
         try {
            Colors colorValue = (Colors) Enum.Parse(typeof(Colors), colorString, true);        
            if (Enum.IsDefined(typeof(Colors), colorValue) | colorValue.ToString().Contains(","))  
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString());
            else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString);
         }
         catch (ArgumentException) {
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString);
         }
      }
   }
}
// The example displays the following output:
//       Converted '0' to None.
//       Converted '2' to Green.
//       8 is not an underlying value of the Colors enumeration.
//       Converted 'blue' to Blue.
//       Converted 'Blue' to Blue.
//       Yellow is not a member of the Colors enumeration.
//       Converted 'Red, Green' to Red, Green.
<Flags> Enum Colors As Integer
   None = 0
   Red = 1
   Green = 2
   Blue = 4
End Enum

Module Example
   Public Sub Main()
      Dim colorStrings() As String = {"0", "2", "8", "blue", "Blue", "Yellow", "Red, Green"}
      For Each colorString As String In colorStrings
         Try
            Dim colorValue As Colors = CType([Enum].Parse(GetType(Colors), colorString, True), Colors)        
            If [Enum].IsDefined(GetType(Colors), colorValue) Or colorValue.ToString().Contains(",") Then 
               Console.WriteLine("Converted '{0}' to {1}.", colorString, colorValue.ToString())
            Else
               Console.WriteLine("{0} is not an underlying value of the Colors enumeration.", colorString)            
            End If                    
         Catch e As ArgumentException
            Console.WriteLine("{0} is not a member of the Colors enumeration.", colorString)
         End Try
      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.
'       Converted 'blue' to Blue.
'       Converted 'Blue' to Blue.
'       Yellow is not a member of the Colors enumeration.
'       Converted 'Red, Green' to Red, Green.

ignoreCaseパラメーターは、この操作は、大文字小文字を区別するかどうかを指定します。The ignoreCase parameter specifies whether this operation is case-sensitive.

こちらもご覧ください

Parse<TEnum>(String, Boolean) Parse<TEnum>(String, Boolean) Parse<TEnum>(String, Boolean) Parse<TEnum>(String, Boolean)

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(System::String ^ value, bool ignoreCase);
public static TEnum Parse<TEnum> (string value, bool ignoreCase) where TEnum : struct;
static member Parse : string * bool -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String, ignoreCase As Boolean) As TEnum

型パラメーター

TEnum

パラメーター

戻り値

TEnum TEnum TEnum TEnum

Parse<TEnum>(String) Parse<TEnum>(String) Parse<TEnum>(String) Parse<TEnum>(String)

public:
generic <typename TEnum>
 where TEnum : value class static TEnum Parse(System::String ^ value);
public static TEnum Parse<TEnum> (string value) where TEnum : struct;
static member Parse : string -> 'Enum (requires 'Enum : struct)
Public Shared Function Parse(Of TEnum As Structure) (value As String) As TEnum

型パラメーター

TEnum

パラメーター

戻り値

TEnum TEnum TEnum TEnum

適用対象