Convert string array to enum on the fly
Asked Answered
A

5

12

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.

Aikens answered 12/12, 2012 at 14:30 Comment(1)
you are assigning the myArray[] values incorrectly why are you hard coding the Enum values..?Rosaleerosaleen
G
12

Sure! This is all you need:

IEnumerable<myEnum> items = myArray.Select(a => (myEnum)Enum.Parse(typeof(myEnum), a));
Gestalt answered 12/12, 2012 at 14:49 Comment(1)
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 objectRetha
I
5

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.

Intercalation answered 14/2, 2017 at 14:33 Comment(0)
A
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?

Adalbertoadalheid answered 12/12, 2012 at 14:32 Comment(3)
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.Aikens
What happens when you bind an array myEnum[] myArray = new [] {myEnum.Ethernet, myEnum.Wireless, myEnum.Bluetooth}; instead?Adalbertoadalheid
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.Aikens
R
1

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
}
Rosaleerosaleen answered 12/12, 2012 at 14:50 Comment(0)
T
0

Use Enum.Parse in the loop for each element in the array.

Tendance answered 12/12, 2012 at 14:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.