I have an enum like:
public enum BlahType
{
blahA = 1,
blahB = 2,
blahC = 3
}
if I have a string with the value 'blahB', is it possible to cast it against the enum BlahType to get the value 2?
I have an enum like:
public enum BlahType
{
blahA = 1,
blahB = 2,
blahC = 3
}
if I have a string with the value 'blahB', is it possible to cast it against the enum BlahType to get the value 2?
Use:
BlahType yourEnumValue = (BlahType) Enum.Parse(typeof(BlahType), "blahB");
and then
int yourIntValue = (int) yourEnumValue;
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() + "<br>";
lblOutput.Text += sunday.ToString() +
". Int value = " +
((int)sunday).ToString() + "<br>";
lblOutput.Text += tgif.ToString() +
". Int value = " +
((int)tgif).ToString() + "<br>";
}
enum test
{
VAL1,
VAL2
}
static void Main(string[] args)
{
test newTest = (test)Enum.Parse(typeof(test), "VAL2");
Console.WriteLine(newTest.ToString());
}
Use this code...
BlahType blah = Enum.Parse(typeof(BlahType), "blahB");
As stated above by a few others you would want to use:
BlahType myEnum = (BlahType)Enum.Parse(typeof(BlahType), "blahB");
int myEnumValue = (int)myEnum;