Set enum variable using int

G

Guest

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.
 
M

MuZZy

Brian said:
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.

Try this:

var1 = (MyEnum)intVar;


It works for me

MuZZy
 
G

Guest

use

var1 = (MyEnum)intVar;

and don't forget to catch the exception because the values coming from
database may be out of your enum range.

HTH
Alex
 
M

Mythran

Brian Haynes said:
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
 
J

Jon Skeet [C# MVP]

Alex K. said:
var1 = (MyEnum)intVar;

and don't forget to catch the exception because the values coming from
database may be out of your enum range.

No exception will be thrown with the above code - enums do no range
checking.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top