rgalgon

I know in the regular framework I could do something like this:

enum Color{Red,Blue,Green,Yellow};
.................
Type colors = typeof(Color);
foreach(string s in Enum.GetNames(colors))
comboBox1.Items.Add(s);

but in CF GetNames() is not available.

Is there some way iterating through an enumeration in the Compact Framework

I'm doing Windows Mobile development in C#.


Re: .NET Compact Framework Iterate Through Enum

Ilya Tumanov

See this.




Re: .NET Compact Framework Iterate Through Enum

rgalgon

Ilya Tumanov wrote:
See this.


That link goes to a "this domain is parked" website with pop-ups.




Re: .NET Compact Framework Iterate Through Enum

CMoebius

This is a copy of the article he is trying to point you to:

The .NET Compact Framework does not include the GetValues() method of the System.Enum method which allows you to return an array of all members of an Enum. However with a bit of reflection it is possible to duplicate this functionality.

[code]public Enum[] EnumToArray(Enum enumeration)
{
//get the enumeration type
Type et = enumeration.GetType();

//get the public static fields (members of the enum)
System.Reflection.FieldInfo[] fi = et.GetFields(BindingFlags.Static | BindingFlags.Public);

//create a new enum array
Enum[] values = new Enum[fi.Length];

//populate with the values
for(int iEnum = 0; iEnum < fi.Length; iEnum++)
{
values[iEnum] = (Enum)fi[iEnum].GetValue(enumeration);
}

//return the array
return values;
}[/code]

You can call the function with an instance of an enumeration to return an array of all values e.g.

[code]Enum[] myvalues = EnumToArray(new System.UriHostNameType());[/code]

Peter Foot, eMVP





Re: .NET Compact Framework Iterate Through Enum

Michael Koster

The OpenNETCF SDF features a class called Enum2 (for SDF 2.0) or EnumEx (for SDF 1.4) which provides the missing functionalities.

More infos are available here: http://www.opennetcf.com/CompactFramework/Products/SmartDeviceFramework/tabid/65/Default.aspx

Michael