-4

ASP.NET アプリケーションがあり、if ステートメントで列挙型を使用したいと考えています。

私はこの方法で変数を取得します:

string choice = (string)Session["export_choice"];
if(choice == <here goes enum>)
{
}
else
{
}

enum は 2 つの文字列値のみを持つことができます。

4

2 に答える 2

0

これは通常、私が使用する場所ですswitch

myEnumType myEnumChoice;

if (Enum.TryParse(choice, out myEnumChoice))
{
    switch(myEnumChoice)
    {
        case myEnumType.FirstEnum:
            //doSomething();
            break;
        case myEnumType.SecondEnum:
            //doSomethingElse();
            break;
    }
}
else
    throw new ArgumentException(string.Format("Unexpected enum value: {0}", choice));
于 2013-04-17T11:55:03.400 に答える