星期三, 5月 28, 2008

[C#] Convert a string to enum

好久沒開始C and P了 (Copy and Paste) 來記Blog....

現在開始吧.......

from Mark Wagner's .NET C# Cogitation
Convert a string to an enum

private enum Aircraft
{
Beech,
Cessna,
Piper
}


You can easily convert the string to an enum value like this:




Aircraft air = (Aircraft) Enum.Parse(typeof(Aircraft), "Cessna", true);



Ideally you should wrap a try-catch around the Enum.Parse statement.

rated (enum) value