none
Getting the numeric value of an enum

    Question

  • Hello,

    I have an enum object and I need to get its numeric value. The problem is that I don't know its underlying type in advance, so I can't just use (int)enumObject to get it. I can use Enum.GetUnderlyingType to get the type, but then how do I cast the enum object to that type, or is there another way to do it?

    Thanks.

     

    Saturday, May 21, 2011 6:01 PM

Answers

  • Hi If your purpose is just using the value then you can get it as an object or precisely use a switch case checking its underlying type.

      public enum Alphabets:long
      {
        A=500000000000,
        B=100000000000,
        C=150000000000
    
      }
    
        private void button1_Click(object sender, EventArgs e)
        {
          Alphabets enm = Alphabets.B;
          test(enm);
        }
    
        private void test(Alphabets enm)
        {
          Type type=Enum.GetUnderlyingType(typeof(Alphabets));
          object obj=Convert.ChangeType(enm, type);
          MessageBox.Show(obj.ToString());
         }
    

    • Proposed as answer by Shane_B Saturday, May 21, 2011 8:49 PM
    • Marked as answer by OLAPWisdom Sunday, May 22, 2011 7:50 AM
    Saturday, May 21, 2011 8:15 PM

All replies

  • I'm confused.  You should be able to cast any enum to int/uint (or long/ulong if the values are larger than 32 bits) without an issue.  You can also use myenum.GetTypeCode() which returns an enum telling you what the underlying type of the enumeration is (IE: int, short, long, etc).  Knowing that you can use a switch statement to do whatever you want.

    ShaneB



    Saturday, May 21, 2011 6:20 PM
  • Use the GetHashCode() method to retrieve the number of the enum.

     

    enumObject,GetHashCode()


    Ali Hamdar (alihamdar.com - www.ids.com.lb)
    Saturday, May 21, 2011 6:49 PM
  • The GetHashCode method is an interesting idea. I would prefer a more direct way rather than using a function that just happens to give me what I need, but I might try it if I don't find any other way, thanks.

     

    Saturday, May 21, 2011 7:21 PM
  • I checked the GetHashCode function, and it returns an int. What if I have a bigger data type for the enum? I actually need to get the result as an Object that contains the boxed value.

     

    Saturday, May 21, 2011 7:28 PM
  • Hello,

    Can you explain the reason you need to know the underlying type in advance ?


    Eyal (http://shilony.net), Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.
    Saturday, May 21, 2011 7:40 PM
  • generally we expect int/uint.

    still you don't want to take risk, expect long/ulong that can hold any underlying type which your enum can have.

    e.g. If i have an enum WeekDays

     long abc= (long)WeekDays.Tuesday;

    FYI. Underlying types can be anyone of the below

    byte, sbyte, short, ushort, int, uint, long, ulong


    public class nishakant:IWantToHelp, INeedHelp
    Saturday, May 21, 2011 7:54 PM
  • To me it hints on a bad design, why would you need to know the underlying type of the enum ? are you trying to create some sort of generic solution to something ?


    Eyal (http://shilony.net), Regards.

    Any fool can write code that a computer can understand. Good programmers write code that humans can understand. -- Martin Fowler.

    Saturday, May 21, 2011 8:06 PM
  • Hi If your purpose is just using the value then you can get it as an object or precisely use a switch case checking its underlying type.

      public enum Alphabets:long
      {
        A=500000000000,
        B=100000000000,
        C=150000000000
    
      }
    
        private void button1_Click(object sender, EventArgs e)
        {
          Alphabets enm = Alphabets.B;
          test(enm);
        }
    
        private void test(Alphabets enm)
        {
          Type type=Enum.GetUnderlyingType(typeof(Alphabets));
          object obj=Convert.ChangeType(enm, type);
          MessageBox.Show(obj.ToString());
         }
    

    • Proposed as answer by Shane_B Saturday, May 21, 2011 8:49 PM
    • Marked as answer by OLAPWisdom Sunday, May 22, 2011 7:50 AM
    Saturday, May 21, 2011 8:15 PM
  • Thanks, for some reason I was sure Enum doesn't implement IConvertible, so I thought I couldn't use Convert.ChangeType, but I checked it now and you are right, so I think I'll use this solution.
    Sunday, May 22, 2011 7:53 AM