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.

1
  • you are assigning the myArray[] values incorrectly why are you hard coding the Enum values..?
    – MethodMan
    Commented Dec 12, 2012 at 14:39

5 Answers 5

12

Sure! This is all you need:

IEnumerable<myEnum> items = myArray.Select(a => (myEnum)Enum.Parse(typeof(myEnum), a));
1
  • 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 object
    – MarkG
    Commented Jan 8, 2014 at 9:45
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.

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?

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.
    – new bie
    Commented Dec 12, 2012 at 15:00
  • What happens when you bind an array myEnum[] myArray = new [] {myEnum.Ethernet, myEnum.Wireless, myEnum.Bluetooth}; instead? Commented Dec 12, 2012 at 15:12
  • 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.
    – new bie
    Commented Dec 12, 2012 at 15:26
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
}
0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.