3

So I have an enum:

public enum myEnum
{
    IBM = 1,
    HP = 2,
    Lenovo = 3
}

I have a Brand class

public class Brand
{
    public Brand(string name, int id)
    {
        Name = name;
        Id = id;
    }

    public string Name { get; private set; }
    public int Id { get; private set; }
}

I want to create a List of Brand objects populated with the data from the MyEnum. Something like:

private IEnumerable<Brand> brands = new List<Brand>
{
    new Brand(myEnum.IBM.ToString(), (int) myEnum.IBM),
    new Brand(myEnum.HP.ToString(), (int) myEnum.HP),
    new Brand(myEnum.Lenovo.ToString(), (int) myEnum.Lenovo),
};

I can create two arrays - one with enum names and the other with enum ids and foreach them to create Brand object for every iteration but wonder if there is a better solution.

Finally, I am going with Royi Mindel solution as according to me it is the most appropriate . Many thanks to Daniel Hilgarth for his answer and his help making Royi Mindel suggestion work. I would give credit to both of them for this question if I could.

public static class EnumHelper
{
    public static IEnumerable<ValueName> GetItems<TEnum>() 
        where TEnum : struct, IConvertible, IComparable, IFormattable
    {
        if (!typeof(TEnum).IsEnum)
            throw new ArgumentException("TEnum must be an Enumeration type");

        var res = from e in Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
                  select new ValueName() { Id = Convert.ToInt32(e), Name = e.ToString() };

        return res;
    }
}

public struct ValueName
{
    public int Id { get; set; }
    public string Name { get; set; }
}
krlzlx
  • 5,752
  • 14
  • 47
  • 55
Mdb
  • 8,338
  • 22
  • 63
  • 98

3 Answers3

13
var brands = Enum.GetValues(typeof(myEnum))
                 .Cast<myEnum>()
                 .Select(x => new Brand(x.ToString(), (int)x))
                 .ToArray();
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
  • 4
    The downvoter is encouraged to leave a comment, so I can fix what ever you think is incorrect. Hint: I tested this code and it works. – Daniel Hilgarth Oct 07 '13 at 08:49
3

I would suggest making a general ValueName class to fit all enums and not make specific classes to fit specific enums

public static class EnumHelper
{
    public static IEnumerable<ValueName> GetItems<TEnum>() 
        where TEnum : struct, IConvertible, IComparable, IFormattable
    {
        if (!typeof(TEnum).IsEnum)
            throw new ArgumentException("TEnum must be an Enumeration type");

        var res = from e in Enum.GetValues(typeof(TEnum)).Cast<TEnum>()
                    select new ValueName() { Value = Convert.ToInt32(e), Name = e.ToString()};

        return res;
    }

}

public struct ValueName
{
    public int Value { get; set; }
    public string Name { get; set; }
}

then you just do :

EnumHelper.GetItems<myEnum>()
Royi Mindel
  • 1,258
  • 12
  • 35
  • I like your generic solution. But I need the Value property to be of type int, and I am unable to cast: select new ValueName() { Value = (int)e, Name = e.toString() }. Suggestions ? – Mdb Oct 07 '13 at 09:22
  • It gives error - Cannot cast expression of type 'System.Enum' to type TEnum. – Mdb Oct 07 '13 at 09:43
  • No, same error. Thanks anyway. I will go with Daniel Hilgarth way. Really like your approach though. – Mdb Oct 07 '13 at 10:38
  • 2
    To make it work, simply replace `Cast` with `Cast` and remove the strange `GetEnumInt` method with a simple `Convert.ToInt32`. Check my answer for reference. – Daniel Hilgarth Oct 07 '13 at 10:43
  • Updated the answer to the working one, thanks @DanielHilgarth – Royi Mindel Oct 07 '13 at 12:07
  • @RoyiMindel: That doesn't work. You need to use `Convert.ToInt32` instead of a simple cast. – Daniel Hilgarth Oct 07 '13 at 12:08
2

Your code was very good, but I was change bit level simplification for your code.

Enum class (Does Not changed):

public enum myEnum
{
    IBM = 1,
    HP = 2,
    Lenovo = 3
}

Brand class(Change parameter length, you can pass one parameter of Enum):

public class Brand
{
    public Brand(Enum Data)
    {
        Name = Data.ToString();
        Id = Convert.ToInt32(Data); 
    }

    public string Name { get; private set; }
    public int Id { get; private set; }
}

And here is change this way:

private IEnumerable<Brand> brands = new List<Brand>
{
    new Brand(myEnum.IBM),
    new Brand(myEnum.HP),
    new Brand(myEnum.Lenovo),
};

And You like see this sample : C# Iterating through an enum? (Indexing a System.Array)

Community
  • 1
  • 1
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
  • 3
    Thank You for your answer, Ramesh! I believe with my way the Brand class interface and usage is more clearer - it accepts name and id and initialize the corresponding properties. – Mdb Oct 07 '13 at 08:55