
 Rank: Guest Groups: Guest
Joined: 9/17/2007 Posts: 11,670 Points: -1,200
|
Date parsed: 06/10/2007 02:02:49 Date: Fri, 5 Oct 2007 16:02:49 +1000
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)); } }
|

 Rank: Guest Groups: Guest
Joined: 9/17/2007 Posts: 11,670 Points: -1,200
|
Date parsed: 06/10/2007 02:09:40 Date: Fri, 5 Oct 2007 16:09:40 +1000
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" <daniel_c_jeffrey@hotmail.com> wrote in message news:##DDpVxBIHA.1208@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)); > } > } > > >
|