3

I have a class with 3 enum types

I want to have a method that can have all 3 of those enums as a parameter and get the integer value of the enum.

public enum Enum1
{
    Fire = 0,
    Hour_24 = 1,
    Key_Switch = 2,
    Follower = 3,
    Entry_Delay1 = 4,
    Entry_Delay2 = 5,
    Intertior = 6,
    Local_Only = 7,
}

public enum Enum2
{
    Faulted = 0,
    Tampered = 1,
    Trouble = 2,
    Bypassed = 3,
    Inhibited = 4, 
    Low_Battery = 5,
    Loss_Supervision = 6,
    Reserved,
    Alarm_Memory = 8,
    Bypass_Memory = 9
}

private void BuildMessage ()
{
     List<Enum1> Enum1List = new List<Enum1>();
     FillBits(Enum1List);  // => Here I get an error.
}

// This method should accept Enum1 and Enum2
private Byte[] FillBits(List<Enum> EnumList)
{
     foreach (Enum e in EnumList)
     {
        int value = Convert.ToInt32(e);
     }
}

How can I achieve this?

Thanks

Robby Smet
  • 4,649
  • 8
  • 61
  • 104

5 Answers5

7

Just use a generic :

private Byte[] FillBits<T>(List<T> EnumList)
        where T : struct, IConvertible
{
    if (!typeof(T).IsEnum) 
    {
        throw new ArgumentException("T must be an enumerated type");
    }
    foreach (var e in EnumList)
    {
        int value = Convert.ToInt32(e);
    }
}

See this question for using generic and Enum together :

Create Generic method constraining T to an Enum

Community
  • 1
  • 1
Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
2

Since the enum already has a int value, why not just cast it to int?

ferdyh
  • 1,355
  • 2
  • 11
  • 29
  • Sorry, maybe this an unclear example but this is a very simplified version of my problem. I just want to know how I can pass multiple types of enum. – Robby Smet Apr 17 '13 at 08:39
  • Then use classes or pass 3 lists of enums and loop through each list. – ferdyh Apr 17 '13 at 08:40
2

Try using object maybe?

private void BuildMessage()
{
    var enum1List = new List<object>();
    FillBits(enum1List);  // => Here I get an error.
}

// This method should accept Enum1 and Enum2
private Byte[] FillBits(IEnumerable<object> enumList)
{
    foreach (Enum e in enumList)
    {
        int value = Convert.ToInt32(e);
    }
}
Yeronimo
  • 1,731
  • 15
  • 28
2

This should work (No checks or whatever added, just basic functionality):

private void BuildMessage()
{
    List<Enum1> Enum1List = new List<Enum1>();
    Enum1List.Add(Enum1.Fire);
    Enum1List.Add(Enum1.Follower);
    FillBits(Enum1List);
}

private Byte[] FillBits<T>(List<T> enumList)
{
    foreach (var e in enumList)
    {
        int value = Convert.ToInt32(e);
    }
}
Ken de Jong
  • 219
  • 1
  • 11
  • So you can use this method with any type? That would fail if `T` is not convertible to int. And where are you using the other enum types? He wants to use a list with all enum types not only `Enum1`. – Tim Schmelter Apr 17 '13 at 08:48
  • Like i said, it has no checks included, it is just basic functionality ;) There are ways to make this safe ofcourse :) – Ken de Jong Apr 17 '13 at 08:50
2
private Byte[] FillBits<T>(List<T> EnumList) where T: struct, IConvertable
{
     foreach (Enum e in EnumList)
     {
        int value = Convert.ToInt32(e);
     }
}

Because you cannot inherit with Enums you have no real way of ensuring that the function can only be called with a List<Enum1>, List<Enum2> or List<Enum3> at compile level. You have two real alternatives

1) Runtime Type checking

private Byte[] FillBits<T>(List<T> EnumList) Where T:struct, IConvertable
{
     if (typeof(T) != typeof(Enum1) && 
         typeof(T) != typeof(Enum2) && 
         typeof(T) != typeof(Enum3)) {

         throw new <EXCEPTION OF YOUR CHOICE!>;
     }

     foreach (Enum e in EnumList)
     {
        int value = Convert.ToInt32(e);
     }
}

Or move FillBits<T> into a base class so it cannot be called directly and provide protected overloads.

private Byte[] PrivateFillBits<T>(List<T> EnumList) where T: struct, IConvertable
{ ... }

protected Byte[] FillBits(List<Enum1> EnumList) {
   return this.PrivateFillBits(EnumList);
}

protected Byte[] FillBits(List<Enum2> EnumList) {
   return this.PrivateFillBits(EnumList);
}

protected Byte[] FillBits(List<Enum3> EnumList) {
   return this.PrivateFillBits(EnumList);
}
Bob Vale
  • 18,094
  • 1
  • 42
  • 49