次の方法で共有


Enum.Parse メソッド (Type, String, Boolean)

文字列形式での 1 つ以上の列挙定数の名前または数値を、等価の列挙オブジェクトに変換します。演算で大文字と小文字を区別するかどうかをパラメータで指定します。

Overloads Public Shared Function Parse( _
   ByVal enumType As Type, _   ByVal value As String, _   ByVal ignoreCase As Boolean _) As Object
[C#]
public static object Parse(TypeenumType,stringvalue,boolignoreCase);
[C++]
public: static Object* Parse(Type* enumType,String* value,boolignoreCase);
[JScript]
public static function Parse(
   enumType : Type,value : String,ignoreCase : Boolean) : Object;

パラメータ

  • enumType
    列挙体の Type
  • value
    変換する名前または値が含まれている文字列。
  • ignoreCase
    true の場合は大文字と小文字が区別されません。それ以外の場合は大文字と小文字が区別されます。

戻り値

値が value により表される enumType 型のオブジェクト。

例外

例外の種類 条件
ArgumentNullException enumType または value が null 参照 (Visual Basic では Nothing) です。
ArgumentException enumTypeEnum ではありません。

または

value が空の文字列 ("") であるか、または空白しか含まれていません。

または

value は名前ですが、列挙体に対して定義された名前付き定数ではありません。

解説

value パラメータには、値、名前付き定数、またはコンマ (,) で区切られた名前付き定数のリストが含まれています。value 内のそれぞれの値、名前、またはコンマの前または後ろに 1 つ以上の空白を付けることができます。value がリストの場合、戻り値は、指定した名前をビットごとの OR 演算で組み合わせた結果の値になります。

演算で大文字と小文字を区別するかどうかを ignoreCase パラメータで指定します。

使用例

Parse を使用して、パラメータとして TypeString を受け取る方法については、次のコード例を参照してください。

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

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, Common Language Infrastructure (CLI) Standard

参照

Enum クラス | Enum メンバ | System 名前空間 | Enum.Parse オーバーロードの一覧