3

I know that I can convert an int to an enum using a cast

MyEnumType myEnum = (MyEnumType) myInteger;

The problem here is that the runtime cast won't stop me at build time if myInteger is not of type int

void MyMethod(MyObject myObject)
{
    MyEnumType myEnum = (MyEnumType) myObject.someProperty;
    ....
}

The above is not an uncommon code pattern but it won't protect me at build-time if the object's property type has been changed.

Is there a built-in method to do this conversion that will give me a build-time error? I could, of course, write a generic method rather easily but I'm wondering if one is built in.

11
  • 1
    Even if that were possible, how could you expect to get a build-time error when performing a cast of a value that is only known at run-time? What other "non-int" types are you expecting to convert from? You cannot cast any arbitrary object to an enum. Commented Nov 13, 2013 at 17:50
  • @JeffMercado my hope is not to perform the cast. A typed method would give me compile-time feedback since I am not relying on a cast. Commented Nov 13, 2013 at 17:51
  • i think "Enum.TryParse(myInteger.ToString())" should work. (HD repairing, no way to test here). Commented Nov 13, 2013 at 17:54
  • 1
    @Servy yes, that's precisely my point. I don't want my code to compile. But if someProperty is of type object then it will compile. Commented Nov 13, 2013 at 18:04
  • 1
    Given than an enum can be any integral type (byte, sbyte, short, ushort, int, uint, long, or ulong), it wouldn't be possible to check for overflow at compile time, so why would it help to check the type? Commented Nov 13, 2013 at 18:40

2 Answers 2

2

You can use

Enum.TryParse(myInteger.ToString(), out result)

to get the Enum value of an int.

Hope this helps,

Sign up to request clarification or add additional context in comments.

7 Comments

That's not how you call TryParse. Additionally, it's for taking a string that represents the name of one of the enum's values, not the integer representation, so this wouldn't work even if you fixed how you call it.
@Servy according to MSDN you can call TryParse using the integer value, too. I didn't know that, either. msdn.microsoft.com/en-us/library/dd783499(v=vs.110).aspx
According to the MSDN docs msdn.microsoft.com/en-us/library/dd783499(v=vs.110).aspx it does take the name OR underlying value
In any case, this doesn't apply static typing at all. The goal is to ensure that you can only compile the code when you know the conversion will succeed, so that if the programmer calls the method on something of the wrong type they get a compile time error, rather than a bug at runtime. This method doesn't do that. You can call ToString on anything. This is no better than casting.
It may not throw a compile time error, but it will also not throw a runtime error....
|
2

You can create a method to do this for your one enum easily enough:

public static MyEnumType CastFromInt<T>(int n)
{
    return (MyEnumType)n;
}

Sadly, because there is no way to apply a generic constraint such that a generic type argument is an enumeration, there's no good way to genericize this. You could write something like this:

public static T CastFromInt<T>(int n)
{
    return (T)(object)n;
}

but that assumes the caller uses an enum as the type of T. If they don't, it has problems. This also needlessly boxes the integer.

7 Comments

The OP asked for a "build-time error". This will not fail at compile time. This will actually hide errors that otherwise would have occurred at compile time such as MyEnumType = 3. What the OP wants to do simply is not possible.
@P.Brian.Mackey The first one is exactly what the OP is looking for, it just needs to be written for each type of enum. The second will only fail at runtime if a non-enum value is used for T, or an enum that's not backed by an integer is used for T. In the case of the first method there is no call to the method that will compile and not successfully convert the integer to the given enumeration.
MyEnumType e = 3 => Compile time error if this value is not valid. MyEnumType e = CastFromInt<MyEnumType>(3) => Runtime error. Downgrade.
@P.Brian.Mackey That second snippet won't throw an error at runtime. It will return an instance of that enumeration representing the value 3. If the enum is backed by an int anyway. What makes you think that'll fail? For the record, MyEnumType e = 3 always fails to compile, there is no "if there isn't a valid value". It will never compile, ever.
Oh nm, sorry I screwed up something in my test.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.