public void EnumInstanceFromString()
{
DayOfWeek wednesday =
(DayOfWeek)Enum.Parse(typeof(DayOfWeek), "Wednesday");
DayOfWeek sunday =
(DayOfWeek)Enum.Parse(typeof(DayOfWeek), "sunday", true);
DayOfWeek tgif =
(DayOfWeek)Enum.Parse(typeof(DayOfWeek), "FRIDAY", true);
lblOutput.Text = wednesday.ToString()
+ ". Int value = " + ((int)wednesday).ToString() + "
";
lblOutput.Text += sunday.ToString()
+ ". Int value = " + ((int)sunday).ToString() + "
";
lblOutput.Text += tgif.ToString()
+ ". Int value = " + ((int)tgif).ToString() + "
";
}
The Enum.Parse method takes two or three arguments. The first is the
type of the enum you want to create as output. The second field is the
string you want to parse. Without a third input, the case of the input
string must match an enum instance or the conversion fails. But the
third input indicates whether to ignore case. If true, than wEdNEsDAy
will still get converted successfully.
Example: Output
Wednesday. Int value = 3
Sunday. Int value = 0
Friday. Int value = 5
0 comments:
Post a Comment