1

I have these enums:

public enum TestEnum1{
    Null = 0,
    A,
    B,
    C,
}

public enum TestEnum2{
    Null = 0,
    D,
    E,
    F,
}

public enum TestEnum3{
    Null = 0,
    A = TestEnum1.A,
    B = TestEnum1.B,
    D = TestEnum2.D,
}

In the above case, TestEnum3 reuses values from TestEnum 1 & 2 and some of the values will overlap. One easy way would be to make TestEnum1.A & B = some number but if you have many enums this seems like it could cause problems.

Galma88
  • 2,398
  • 6
  • 29
  • 50
  • 4
    It doesn't really re-use them - they might have the same integer values, but they're still different. What are you actually trying to do? Maybe an `enum` isn't the right solution to your problem. – Charles Mager Jun 30 '15 at 14:04
  • 4
    Replace the semicolons with commas to make it at least compile. – Tim Schmelter Jun 30 '15 at 14:05
  • possible duplicate of [Enum "Inheritance"](http://stackoverflow.com/questions/757684/enum-inheritance) – Sinatr Jun 30 '15 at 14:16

3 Answers3

2

Try this

    public enum TestEnum1{
    Null = 0,
    A,
    B,
    C,
    Last
}

    public enum TestEnum2{
    Null = 0,
    D = TestEnum1.Last,
    E,
    F,
    Last
}

public enum TestEnum3{
    Null = 0,
    A = TestEnum2.Last,
    B, 
    D,
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

Enumerations are value type in C#.

The code you outlined does not reuse any values you are just assigning one variable from another variable.

IMHO, the best practice would be to assign the integer value directly instead using an integer value from another Enum.

Umamaheswaran
  • 3,690
  • 3
  • 29
  • 56
0

You can already see the problems mixing enums together will cause, hence your question.
So I think the "best practice" would be... don't do it.

If you really must merge them somehow, you could make a struct containing one Enum or the other.

public struct EnumsCombined
{
     public EnumsCombined(TestEnum1 v) { Enum1 = v; }
     public EnumsCombined(TestEnum2 v) { Enum2 = v; }

     public TestEnum1? Enum1 { get; private set; }
     public TestEnum2? Enum2 { get; private set; }
}