none
Using a generic list to hold various Enum types RRS feed

  • Question

  • I have a need to display certain Enums as a list so they can be added to a ComboBox. What I've been doing is creating a property for each Enum that turns each element of the Enum into an item in a list. It works great, but I'd rather just have one function that outputs a list based on whatever Enum I send it.

    For example, here's how I do it now for an Enum named Importance:

    public List<Importance> ImportanceTypes
            {
                get
                {
                    List<Importance> importanceTypes = new List<Importance>();
    
                    foreach (string importanceType in Enum.GetNames(typeof(Importance)))
                    {
                        importanceTypes.Add((Importance)Enum.Parse(typeof(Importance), importanceType));
                    }
    
                    return importanceTypes;
                }
            }

    What I'd like to have is something like this, where GenericEnumType is whatever Enum I want it to be...

    public static List<GenericEnumType> EnumToList(GenericEnumType genericEnumType) {
        List<genericEnumType> enumTypes = new List<genericEnumType>();
    
        foreach (string enumType in Enum.GetNames(typeof(genericEnumType))) {
    
            enumTypes.Add((genericEnumType)Enum.Parse(typeof(genericEnumType), enumType));
    
        }
    
        return enumTypes;
    }

    And then I can just feed that function an Enum type and get a List to populate ComboBoxes, etc... with.

    Is there a good way to allow for generic Enums in a function like that?

    Tuesday, April 30, 2019 8:17 PM

Answers

  • The following will work, but if you pass a type that is not an enum it will fail at runtime, so perhaps someone has an idea to avoid that:

    	public static List<System.Enum> EnumToList(System.Type genericEnumType)
    	{
    		List<System.Enum> enumTypes = new List<System.Enum>();
    
    		foreach (string enumType in Enum.GetNames(genericEnumType))
    		{
    			enumTypes.Add((System.Enum)Enum.Parse(genericEnumType, enumType));
    		}
    
    		return enumTypes;
    	}

    You can call the method like this:

    List<System.Enum> test = EnumToList(typeof(TestEnum));



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter


    • Edited by Dave Doknjas Tuesday, April 30, 2019 8:40 PM
    • Marked as answer by T Gregory Tuesday, April 30, 2019 10:21 PM
    Tuesday, April 30, 2019 8:39 PM

All replies

  • The following will work, but if you pass a type that is not an enum it will fail at runtime, so perhaps someone has an idea to avoid that:

    	public static List<System.Enum> EnumToList(System.Type genericEnumType)
    	{
    		List<System.Enum> enumTypes = new List<System.Enum>();
    
    		foreach (string enumType in Enum.GetNames(genericEnumType))
    		{
    			enumTypes.Add((System.Enum)Enum.Parse(genericEnumType, enumType));
    		}
    
    		return enumTypes;
    	}

    You can call the method like this:

    List<System.Enum> test = EnumToList(typeof(TestEnum));



    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter


    • Edited by Dave Doknjas Tuesday, April 30, 2019 8:40 PM
    • Marked as answer by T Gregory Tuesday, April 30, 2019 10:21 PM
    Tuesday, April 30, 2019 8:39 PM
  • Perfect. This makes sense and is what I was looking for. Thanks!
    Tuesday, April 30, 2019 10:21 PM