Cannot convert type 'System.Enum' to int

35,143

Solution 1

System.Enum cannot be directly cast to Integer, but it does explicitly implement IConvertible, meaning you can use the following:

public void AddLink(Enum enumVal)
{
    string identifier = m_EnumInterpreter(Convert.ToInt32(enumVal));
    AddLink(identifier);
}

Keep in mind that if your Enum is actually using something other than an Integer (such as a float), you'll lose the non-integer data on conversion. Or obviously replace the Convert call with whatever you are converting from (if it's known)

Solution 2

No, you aren't able to cast it to an int because System.Enum is not an enum, it's just the base class for enums.

EDIT:

You can get the value as follows, but it is ugly:

int intVar = (int)enuYourEnum.GetType().GetField("value__").GetValue(objYourEnum);

Solution 3

try this..

m_EnumInterpreter((int) (object) enumVal);

Solution 4

Various things here:

1) the answer of Ryan looks ok, but... I would rather pass the Enum down to the enum interpreter, so that you can do the whole Convert.To... there. If you know that you are using ONLY integer based enums, the Convert.ToInt32() is just fine. Otherwise you may want to add by using either reflection or try/catch other conversions.

2) You may also consider using members of the Enum class, like .GetName(), .GetValue(), etc. since they deal directly with the defined names and values independent of the enum type.

3) technically I would not throw the exception outside the enum interpreter. If that condition is generally true, throw the exception from inside the enum interpreter, so that all uses of the class will benefit of the validation. Or you might end up duplicating code.

4) you seem to have an C++/MFC background judging from your variable naming. You might want to get into C# style naming conventions, it will ease your life when using/reading other peoples code and libraries. Check out MS's StyleCop for a good addin to help with naming.

Solution 5

I don't know whether to include this in my question, or as an answer. The problem is that it isn't THE answer, but it is the answer that works for me.

What I discovered, by chance while trying something else, is that if I just wodge it onto the end of a string, I get what I want:

throw new Exception("EnumInterpreter returns empty string for enumVal=" + enumVal);
//EnumInterpreter returns empty string for enumVal=3720116125

I actually simplified to int in my question, the real data type is uint (in this particular instance). Fortunately, given that I only actually wanted the string, I don't have to worry about that.

I'm not sure which of the three other answers is 'right', so vote away...

Share:
35,143
tina Miller
Author by

tina Miller

Saved from the web by a job in industry. Coding GUI for computer-aided railway realignment machinery. At last a job where I have time to do stuff properly and don't have to worry about loads of unfixed bugs coming back to haunt me.

Updated on September 02, 2020

Comments

  • tina Miller
    tina Miller over 3 years

    (OK, I'll expose the depths of my ignorance here, please be gentle)

    Background

    I've got a method which looks (a bit) like this:

    public void AddLink(Enum enumVal)
    {
         string identifier = m_EnumInterpreter(enumVal);
         AddLink(identifier);
    }
    

    The EnumInterpreter is a Func<Enum, string> that is passed in when the parent class is created.

    I'm using Enum because at this level it is 'none of my business'- I don't care which specific enum it is. The calling code just uses a (generated) enum to avoid magic strings.

    Question

    If the EnumInterpreter sends back an empty string, I'd like to throw an exception with the actual value of enumVal. I thought I would just be able to cast to int, but it the compiler won't have it. What am I doing wrong? (Please don't say 'everything').