0

In my project i have an enum in which i store Music genres values that looks like this :

enum Genres { Rock,Pop,Disco,Sport,Talk,Metal,Hard_Rock,Electro,Classic_Rock}

Now in my code i want a foreach loop that will do some work based on the music genre.Something like this:

foreach(Genres genre in Enum.GetValues(typeof(Genres)))
{
     switch(genre)
     {
         case Genres.Disco:
                 //Do Something
              break;
             case Genres.Rock:
                 //Do Something
              break;
         .....
         .......
     }
}

What i want to do treat my enum in the switch case like an array.Something like this:

foreach(Genres genre in Enum.GetValues(typeof(Genres)))
{
     switch(genre)
     {
         case Genres[0]:
                 //Do Something
              break;
             case Genres[1]:
                 //Do Something
              break;
         .....
         .......
     }
}

Is this possible? Thanks in advance.

EDIT

I have a ListBox in which i want to populate it with GenreItems. But for every ListBoxItem i want to have a diffent name ass i pass them in. So i make this switch in order to check the genre and case is Rock for example i set the ListBoxItem as ROCK and add it in the ListBox.

oimitro
  • 1,466
  • 4
  • 14
  • 22
  • 1
    Why the foreach?( you can just switch via enum) – Sayse Aug 08 '13 at 09:48
  • 1
    can you tell us what exactly you are trying to do, there might be a much better solution to this than having an array – x4rf41 Aug 08 '13 at 09:49
  • I think he want's something like this http://stackoverflow.com/questions/482729/c-sharp-iterating-through-an-enum-indexing-a-system-array? Possible duplication or at least similar? – butterbox Aug 08 '13 at 09:58

3 Answers3

2

I don't think it's really possible unless you do some tweaks... for example, use a static array to store all the values:

public static readonly Genres[] AllGenres = Enum.GetValues(typeof(Genres)).Cast<Genres>().ToArray();

// sample:
public void test()
{
   var first = AllGenres [0];
}

EDIT

Now I understand what you need. you actually want a list to bind to a control. I think you might want to try use attribute to your enum if you think you only need a description, and then write a generic method to retrieve enum items and wrap it as an object, something like:

public enum Genres
{
    [Description("Rock!!!")]
    Rock, 
    Pop, 
    Disco, 
    Sport, 
    Talk, 
    Metal, 
    Hard_Rock, 
    Electro, 
    [Description("Classic Rock")]
    Classic_Rock
}

public class EnumItem
{
    string Name { get; set; }
    string Description { get; set; }
    Enum EnumValue {get;set;}

    public static IEnumerable<EnumItem> GetValue(Type t)
    {
        // implementation using reflection/expression            
    }
}

// then you can use the retrieved list/whatever to do binding or so...

you may further tweak this to meet your needs (for example, using type constraint to make it better) - and more important, this will become a generic class to serve all enum types...

iCollect.it Ltd
  • 92,391
  • 25
  • 181
  • 202
Rex
  • 2,130
  • 11
  • 12
1

According to your edit it looks like you want

foreach(var genre in Enum.GetNames(typeof(Genres)))
{
     listBox.Items.Add(genre.ToUpperInvariant());
}
Sayse
  • 42,633
  • 14
  • 77
  • 146
0

are you trying to do this?

                foreach(Genres genre in Enum.GetValues(typeof(Genres)))
                {
                    switch (genre)
                    {
                        case Genres.Classic_Rock:
                            //your code
                            break;
                        case Genres.Disco:
                            //your code
                            break;
                        default:
                            //your code
                            break;
                    }
                }
Mujah Maskey
  • 8,654
  • 8
  • 40
  • 61