Convert string array to enum on the fly

23,131

Solution 1

Sure! This is all you need:

IEnumerable<myEnum> items = myArray.Select(a => (myEnum)Enum.Parse(typeof(myEnum), a));

Solution 2

If your source data is not something entirely reliable, you may want to consider converting only the items that can actually be parsed, using TryParse() and IsDefined().

Getting an array of myEnums from an array of strings can be performed by the following code:

myEnum [] myEnums = myArray
    .Where(c => Enum.IsDefined(typeof(myEnum), c))
    .Select(c => (myEnum)Enum.Parse(typeof(myEnum), c))
    .ToArray();

Note that IsDefined() only works with a single enumerated value. If you have a [Flags] enum, combinations fail the test.

Solution 3

You'll want to use Enum.Parse: http://msdn.microsoft.com/en-us/library/essfb559.aspx

MyProperty = (myEnum)Enum.Parse(typeof(myEnum), myArray[0]);

How you'll want to use that with your array I guess is up to your needs.

EDIT: By any chance, is it feasible to store your adapter names to your array as enumerations in the first place? Is there some reason the array must be strings?

Solution 4

You do not have to use Parse if you want to get the name of the enum's value. Don't use .ToString(), use this instead. For example if I want to return Ethernet I would do the following:

public enum myEnum
{
    Ethernet,
    Wireless,
    Bluetooth
}

In your main class add this line of code:

var enumName = Enum.GetName(typeof(myEnum), 0); //Results = "Ethernet"

If you want to enumerate over the Enum Values you could do this to get the values:

foreach (myEnum enumVals in Enum.GetValues(typeof(myEnum)))
{
    Console.WriteLine(enumVals);//if you want to check the output for example
}
Share:
23,131
new bie
Author by

new bie

I love to learn and share various things about the hardware and software

Updated on July 05, 2022

Comments

  • new bie
    new bie almost 2 years

    I am binding an enum to a property grid like this:

    public enum myEnum
    {
        Ethernet,
        Wireless,
        Bluetooth
    }
    
    public class MyClass
    {
        public MyClass()
        {
            MyProperty = MyEnum.Wireless;
        }
    
        [DefaultValue(MyEnum.Wireless)]
        public MyEnum MyProperty { get; set; }
    }
    
    public Form1()
    {
        InitializeComponent();
        PropertyGrid pg = new PropertyGrid();
        pg.SelectedObject = new MyClass();
        pg.Dock = DockStyle.Fill;
        this.Controls.Add(pg);
    }
    

    My problem: I get data on the fly when the program is running. I read the network adapter then store adapter names to myArray like this:

    string[] myArray = new string[] { };
    myArray[0] = "Ethernet";
    myArray[1] = "Wireless";
    myArray[2] = "Bluetooth";
    

    Is possible convert myArray to myEnum on the fly using c#? Thank You.

  • new bie
    new bie over 11 years
    I want binding myArray to Property grid and myArray looks like listbox on Propery grid, same as when binding enum type to property grid. That's why I want to convert the array into enum.
  • Chris Sinclair
    Chris Sinclair over 11 years
    What happens when you bind an array myEnum[] myArray = new [] {myEnum.Ethernet, myEnum.Wireless, myEnum.Bluetooth}; instead?
  • new bie
    new bie over 11 years
    The problem, Enum type never existed. I get data adapter name when program runs and then store it into a string array. If string array binding to the property-grid, will be display a string-collection-editor on property grid, while I want appear like a listbox on the property-grid. That's why I want to convert a string to an enum type.
  • MarkG
    MarkG over 10 years
    Actually you need to cast IEnumerable<myEnum> items = myArray.Select(a => (myEnum) Enum.Parse(typeof (myEnum), a)); otherwise you'll get a compiler error because Enum.Parse() returns an object