Hello.
Can anyone help me please. I have created an Enumeration, and I loop through
it, it all works ok, except the first item returns 2 times.
Code is below. When called I Get "Text Edit" 2 times
public enum CustomFieldTypes
{
[Description("Text Edit")]
TextEdit,
[Description("Date/Time Edit")]
DateEdit,
[Description("Generic Combo Box")]
ComboBox,
[Description("SQL Combo Box")]
SqlComboBox,
[Description("Check Box Edit")]
CheckBox,
[Description("Memo Edit")]
MemoEdit,
[Description("URL Edit")]
URLEdit
};
public static string GetEnumDescription(Enum en)
{
Type type = en.GetType();
MemberInfo[] memInfo = type.GetMember(en.ToString());
if (memInfo != null && memInfo.Length > 0)
{
object[] attrs = memInfo[0].GetCustomAttributes(typeof(Description),
false);
if (attrs != null && attrs.Length > 0)
return ((Description)attrs[0]).Text;
}
return en.ToString();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
foreach (FieldInfo fi in typeof(CustomFieldTypes).GetFields())
{
CustomFieldTypes val = (CustomFieldTypes)fi.GetValue(new
CustomFieldTypes());
MessageBox.Show(Program.GetEnumDescription(val));
}
}
Daniel Jeffrey - 05 Oct 2007 07:09 GMT
Solved it myself.
I converted to
foreach (CustomFieldTypes val in
Enum.GetValues(typeof(CustomFieldTypes)))
{
MessageBox.Show(Program.GetEnumDescription(val));
}
Works much better.
Thanks anyway.
Dan
> Hello.
>
[quoted text clipped - 46 lines]
> }
> }
Ben Voigt [C++ MVP] - 08 Oct 2007 15:27 GMT
> Hello.
>
> Can anyone help me please. I have created an Enumeration, and I loop
> through it, it all works ok, except the first item returns 2 times.
If you disassemble an Enum, (here's DockStyle), you'll see your problem:
.class public auto ansi sealed DockStyle
extends [mscorlib]System.Enum
{
.custom instance void
[System]System.ComponentModel.EditorAttribute::.ctor(string, class
[mscorlib]System.Type) = { string('System.Windows.Forms.Design.DockEditor,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a')
type([System.Drawing]System.Drawing.Design.UITypeEditor) }
.field public static literal valuetype System.Windows.Forms.DockStyle
Bottom = int32(0x2)
.field public static literal valuetype System.Windows.Forms.DockStyle
Fill = int32(0x5)
.field public static literal valuetype System.Windows.Forms.DockStyle
Left = int32(0x3)
.field public static literal valuetype System.Windows.Forms.DockStyle
None = int32(0x0)
.field public static literal valuetype System.Windows.Forms.DockStyle
Right = int32(0x4)
.field public static literal valuetype System.Windows.Forms.DockStyle
Top = int32(0x1)
.field public specialname rtspecialname int32 value__
}
Note that all the enum constants are public static fields, but there's one
additional field which is an instance field holding the actual value. So
use GetFields(BindingFlags.Static | BindingFlags.Public) to avoid pulling in
the value__ instance field.Oh, and for static fields, you don't need to pass
an instance to GetValue(). But when you do, the value__ field is
initialized to zero, hence you see the attribute associated with zero twice.
> Code is below. When called I Get "Text Edit" 2 times
>
[quoted text clipped - 41 lines]
> }
> }