9

Possible Duplicate:
convert an enum to another type of enum

When casting between enums of different types, is it possibly to directly cast from one type to the other like so?

LicenseTypes type;
TypeOfLicense type2;

type2 = (TypeOfLicense)type;

Or is it necessary to cast the enum to an int first? The compiler doesn't complain, but I want to make sure I'm not going to get some kind of funky casting exception at runtime.

This may seem like an odd thing to do, but both enums are identical in structure and value. Only the type names differ in this case.

Community
  • 1
  • 1
codewario
  • 19,553
  • 20
  • 90
  • 159

3 Answers3

9

Although its a duplicate and I have flagged it as well. You can do simple casting, beucase the underlying type is int, you can do:

LicenseTypes type = (LicenseTypes)TypeOfLicense.value3;

Here type will hold value '0';

Suppose you have:

public enum LicenseTypes 
{
    value1,
    value2,
}
public enum TypeOfLicense
{
    value3, 
    value4,
    value5
}

But if you try to Cast TypeOfLicence.value5 then the variable type will hold the int value 2

LicenseTypes type = (LicenseTypes)TypeOfLicense.value5;

You can also look at this link for creating extension methods for conversion between enums. (Its the accepted answer of the similar question)

Community
  • 1
  • 1
Habib
  • 219,104
  • 29
  • 407
  • 436
  • I saw that other question, but only just now did I see the bottom last answer. Everything else was way more complex than it had to be. – codewario Oct 04 '12 at 04:53
  • @BasedAsFunk, I believe you don't even need to cast to `int`, you can use casting like : `LicenseTypes type = (LicenseTypes)TypeOfLicense.value3;` – Habib Oct 04 '12 at 04:54
4

No, but you can do this making a few assumptions ...

LicenseTypes type = LicenseTypes.Type1;
TypeOfLicense type2 = TypeOfLicense.Type2;

type2 = (TypeOfLicense)(int)type;

... and those assumptions are this. We must assume that both LicenseTypes and TypeOfLicense base int for their value. We must also assume that casting the underlying values to one another correlate from a business perspective (but that's really neither here nor there from a technical perspective).

Please note this works for other base types and it is possible to build a more complex conversion that would allow you to cast between different types with conversions.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • That assumption is correct, I have two identical enum types, but the first one isn't visible from a new assembly. Looking back now, there are other ways I could have worked around this (using a straight int would have worked fine) but I no longer have the time to go back and do it the "right" way. – codewario Oct 04 '12 at 04:49
  • @BasedAsFunk: Yep, it is what it is after applications get that far into their life often times and so you just work around them as best you can. – Mike Perrenoud Oct 04 '12 at 04:51
  • +1. if base types are different conversion may lose precision (BasedAsFunk is OK as enums are identical). – Alexei Levenkov Oct 04 '12 at 04:58
0

No matter how you do it, it is merely an assignment of integer to another variable. Remember, enums are internally treated as integers and hence it will make no difference.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • It is not quite true - underlying type is any integer type. I.e. if source type is `enum Noraml{}` destination type is `enum Small:byte{}` than you can lose precision at conversion time. – Alexei Levenkov Oct 04 '12 at 04:56
  • You are right in that case. It holds true when both enums are of different type. – Murtuza Kabul Oct 04 '12 at 04:58