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.

3
  • 1
    Why the foreach?( you can just switch via enum) Commented Aug 8, 2013 at 9: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 Commented Aug 8, 2013 at 9:49
  • I think he want's something like this stackoverflow.com/questions/482729/…? Possible duplication or at least similar? Commented Aug 8, 2013 at 9:58

3 Answers 3

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...

Sign up to request clarification or add additional context in comments.

2 Comments

I didn´t understand the question too, until you explained what oimitro asked ^upvoted.
Ah the description tag is what I've been trying to find.. +1
1

According to your edit it looks like you want

foreach(var genre in Enum.GetNames(typeof(Genres)))
{
     listBox.Items.Add(genre.ToUpperInvariant());
}

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.