"Brian Haynes" <Br*********@discussions.microsoft.com> wrote in message
news:59**********************************@microsof t.com...
I've read all the posts in this forum that I can find that look related to
this issue and I have only found 1 solution that I consider to be a bit of
a
hack. What I want to do is assign a value to an enum variable using an
int.
What I am using right now is something like this:
public enum MyEnum
{
value1,
value2,
value3
};
...
MyEnum var1;
// this value actually comes from a database so I can't substitute the
literal value
int intVar = 1;
var1 = (MyEnum)Enum.Parse(var1.GetType(), intVar.ToString());
Is there a simpler way to do this? It doesn't seem like setting the value
should require converting to string and then parsing and casting.
public enum MyEnum : int
{
Value1 = 0,
Value2,
Value3
};
MyEnum var1 = (MyEnum) 1;
MyEnum var2 = MyEnum.Value2;
MyEnum var3 = (MyEnum) Enum.Parse(typeof(MyEnum), "Value3", true);
The above three can be done.
What I do, as a side note, is store the values in a database as the names of
the enumeration value. For example, I if I had MyEnum (as in the above
example), I would not store a value of 0, 1, or 2 into the database...I
would store the string "Value1", "Value2", or "Value3". I would then use
Enum.Parse() to convert the value to MyEnum. I do it this way "most of the
time" so users don't have to "guess" what those values are when they create
their AdHoc reports.
Hope this helps :)
Mythran