1

I have two Enumerations and a method that takes an enumeration as a parameter. They are called ABC and DEF and the method is called TestMethod(Enum myEnum). Code is below:

public enum ABC
{
    Zero = 0,
    One = 1,
    Two = 2
};

public enum DEF
{
    Three = 3,
    Four = 4,
    Five = 5
};

public int TestEnum(Enum myEnum)
{
    int returnValue = ??? // How do I get the value out of this enum that can be either ABC or DEF?
    bool randomTestBool = returnValue > 3;
    return returnValue
}

public void CallerFunction()
{
    int whatsMyInt = TestEnum(DEF.Four);
}

From CallerFunction() I call TestEnum() function and pass in one of the two (ideally way more) enumerations. I need to find out how to obtain the value in integer format so I can compare it within the function. Now, if this was a single type of an enumeration, i.e. if the function was TestEnum(DEF myDefEnum) then this would be easy, however, the function needs to handle multiple Enum types. Thanks to a previous response, I learned that to get a type of the enum I can do one of two things:

1) bool isThisDef = myEnum is DEF; but I am curious if there is a more universal way than creating a scenario for each and every Enum type. Perhaps something like this

2) Type myEnumType = myEnum.GetType(); but I am not sure what to do with this now. Any help is greatly appreciated. =) By the way, string testString = myEnum.toString(); genereates an error :\

Francis Gagnon
  • 3,545
  • 1
  • 16
  • 25
Lukas
  • 2,885
  • 2
  • 29
  • 31

4 Answers4

4
public int TestEnum(Enum myEnum)
{
    return (int)(object)myEnum;
}
L.B
  • 114,136
  • 19
  • 178
  • 224
  • Why the intermediate cast to *object*? – Eric J. Nov 27 '12 at 23:37
  • 1
    @EricJ. to avoid `Cannot convert type 'System.Enum' to 'int'` – L.B Nov 27 '12 at 23:38
  • 1
    While these answers will no doubt work, I would suggest you ask yourself if you are converting multiple `enum` type values to `int` values, do you have the correct data representation? – SAJ14SAJ Nov 27 '12 at 23:46
  • I can indeed testify to this. I was looking at two answers and LB's included the (object) which made all the difference. Thank you both though and although I will be checking this answer as correct as soon as 15 minute restriction is lifted, I will give you both an upvote. Thank you both kindly for a quick crash course in enumerators and values :) – Lukas Nov 27 '12 at 23:47
  • At this point you might as well use `Convert.ToInt32(myEnum)`. – Trisped Nov 27 '12 at 23:52
  • @Trisped, why wouldn't you post this an an answer? – horgh Nov 27 '12 at 23:55
  • @Lukas not `enumerators`, but `enumerations`. These two are very different! – horgh Nov 27 '12 at 23:57
2

You simply write

int returnValue = (int)(object) myEnum;
Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • 2
    And now you only repeat my answer. – L.B Nov 27 '12 at 23:40
  • 1
    No, we posted within 3 seconds of each other. I corrected my answer. – Eric J. Nov 27 '12 at 23:41
  • 2
    Yes you corrected your answer after you learnt the correct one from other answer. – L.B Nov 27 '12 at 23:42
  • There is nothing wrong with that. SO is not a contest, it is a place to learn and share correct answers. – Eric J. Nov 27 '12 at 23:46
  • 1
    Well there are two options, delete and edit. Both would have resulted in SO having the correct information, You made your choice. – gbtimmon Nov 27 '12 at 23:53
  • @gbtimmon: I edited. My answer. That was missing a cast. Deleting would not change my daily rep since I'm well over the daily cap anyhow. I just do not thing it is right to delete an answer because of a small error that can be easily corrected. – Eric J. Nov 28 '12 at 00:34
1

I guess this behaviour is not as much reliable, as it probably can be changed in further releases of .Net, but it also returns the underlying value:

int returnValue = myEnum.GetHashCode();

P.S.: Using GetHashCode for getting Enum int value, i.e. don't do this...

Community
  • 1
  • 1
horgh
  • 17,918
  • 22
  • 68
  • 123
0

You could try:

int returnValue = Convert.ToInt32(myEnum);

A better option in my mind is to explicitly declare ABC and DEF as integer types:

public enum ABC : int
{
    Zero = 0,
    One = 1,
    Two = 2
};

public enum DEF : int
{
    Three = 3,
    Four = 4,
    Five = 5
};

Then just use int returnValue = (int)myEnum;.
To be safe you would have to check the type of the enum provided so the performance gains would be small if any(unless you can guarantee that all enums which can be passed in are based on the integer type, then no check would be required).

Trisped
  • 5,705
  • 2
  • 45
  • 58