By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,172 Members | 1,735 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,172 IT Pros & Developers. It's quick & easy.

Enum Enumeration issue

P: n/a
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));
}
}

Oct 5 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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

"Daniel Jeffrey" <da**************@hotmail.comwrote in message
news:##**************@TK2MSFTNGP05.phx.gbl...
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));
}
}
Oct 5 '07 #2

P: n/a

"Daniel Jeffrey" <da**************@hotmail.comwrote in message
news:%2******************@TK2MSFTNGP05.phx.gbl...
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(strin g, 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
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));
}
}

Oct 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.