Welcome Guest Search | Active Topics | Members | Log In | Register

Enum Enumeration issue Options · View
Daniel Jeffrey
Posted: Saturday, October 06, 2007 2:02:49 AM


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));
}
}



Daniel Jeffrey
Posted: Saturday, October 06, 2007 2:09:40 AM


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));
> }
> }
>
>
>
Users browsing this topic
Guest


Forum Jump
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Main Forum RSS : RSS

YAFPro Theme Created by Jaben Cargman (Tiny Gecko)
Powered by Yet Another Forum.net version 1.9.1.1 (NET v2.0) - 9/10/2007
Copyright © 2003-2006 Yet Another Forum.net. All rights reserved.
This page was generated in 0.114 seconds.