Mdb
Mdb

Reputation: 8568

Create a List of objects using enum data C#

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; }
}

Upvotes: 3

Views: 6103

Answers (3)

Ramesh Rajendran
Ramesh Rajendran

Reputation: 38663

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)

Upvotes: 2

Royi Mindel
Royi Mindel

Reputation: 1258

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>()

Upvotes: 3

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174299

var brands = Enum.GetValues(typeof(myEnum))
                 .Cast<myEnum>()
                 .Select(x => new Brand(x.ToString(), (int)x))
                 .ToArray();

Upvotes: 13

Related Questions