Contents tagged with enum

  • How to Fill a ListBox/DropDownList from an Enum

    There was a question about this on the Asp.Net forums and after a quick search I didn't find a good generic function so I thought I'd supply one.

    Note: I wanted this to be as broad and useful as possible, so the second parameter is a ListControl which both the ListBox and DropDownList inherit from.

    I also made sure the function would handle enums that had non-contiguous values that didn't necessarily start at zero.

    The function:

    // ---- EnumToListBox ------------------------------------
    //
    // Fills List controls (ListBox, DropDownList) with the text 
    // and value of enums
    //
    // Usage:  EnumToListBox(typeof(MyEnum), ListBox1);
     
    static public void EnumToListBox(Type EnumType, ListControl TheListBox)
    {
        Array Values = System.Enum.GetValues(EnumType);
     
        foreach (int Value in Values)
        {
            string Display = Enum.GetName(EnumType, Value);
            ListItem Item = new ListItem(Display, Value.ToString());
            TheListBox.Items.Add(Item);
        }
    }
     

    Usage:  

    I tested with an existing enum and a custom enum:

    enum CustomColors
    {
        BLACK = -1,
        RED = 7,
        GREEN = 14,
        UNKNOWN = -13
    }
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        EnumToListBox(typeof(DayOfWeek), DropDownList1);
     
        EnumToListBox(typeof(CustomColors), ListBox1);
    }

    Note: I initially tried to get the order of the items in the ListBox to match the order of the items in the enum but both the Enum.GetValues and Enum.GetNames functions return the items sorted by the value of the enum. So if you want the enums sorted a certain way, the values of the enums must be sorted that way. I think this is reasonable; how the enums are physically sorted in the source code shouldn't necessarily have any meaning.

    I hope someone finds this useful.

  • Converting Strings to Enums

    Enums are great. They provide additional type safety and make code more readable.

    Converting enums to strings is trivial. Converting strings to enums is not as straightforward...so here is a code snippet:

        // This enum is in the .Net frameworkpublic 
        enum DayOfWeek
        {    
            Sunday = 0,    
            Monday = 1,   
            Tuesday = 2,   
            Wednesday = 3,    
            Thursday = 4,   
            Friday = 5,   
            Saturday = 6,
        } 
     
        protected void Button1_Click(object sender, EventArgs e)
        {    
            // converting enums to strings is easy    
            String WhatDayItIs = DayOfWeek.Monday.ToString();     
     
            // converting strings to enums is a bit more work    
            DayOfWeek WhatDayItIsDOW;
     
            if (Enum.IsDefined(typeof(DayOfWeek), WhatDayItIs)) 
                WhatDayItIsDOW = (DayOfWeek)Enum.Parse(typeof(DayOfWeek), WhatDayItIs);
        }
     

    I hope you find this information useful.

    Steve Wellens