oimitro
oimitro

Reputation: 1506

C# Treat an enum as an array

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.

Upvotes: 0

Views: 776

Answers (3)

Rex
Rex

Reputation: 2140

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

Upvotes: 2

Sayse
Sayse

Reputation: 43300

According to your edit it looks like you want

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

Upvotes: 1

Mujah Maskey
Mujah Maskey

Reputation: 8804

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

Upvotes: 0

Related Questions