473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Enum Enumeration issue

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
2 2533
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
9
by: AngleWyrm | last post by:
"The C++ Programming Language" by Bjarne Stroustrup, copyright 1997 by AT&T, section 4.8 (pp 77): "A value of integral type may be explicitly converted to an enumeration type. The result of such a...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
2
by: mrhicks | last post by:
Hello all, I have a question about enumerations. Within some requirements data passed back a certain bit field is defined by three bits then in another section the bit field is defined as 4...
5
by: Sriram Rajagopalan | last post by:
Hi, Is the extra comma at the end of an enumerator-list valid according to the C standards? With the gcc compiler the following is valid: enum DAYS {MONDAY, TUESDAY, }day1; gcc does not...
31
by: Michael C | last post by:
If a class inherits from another class, say Form inherits from control, then I can assign the Form to a variable of type Control without needing an explicit conversion, eg Form1 f = new Form1();...
3
by: Sanjay Pais | last post by:
I know that string/char enum is not possible in c# (.NET2.0) I need to create the equivalent of this: public enum HOW_GOOD { AWESOME = "A", GREAT= "G", NOT_TOO_BAD = "N", TERRIBLE="T" }
0
by: Ben Finney | last post by:
Howdy all, I've uploaded enum 0.3 to the Cheeseshop. <URL:http://cheeseshop.python.org/pypi/enum/> Enumerations are now sequences, iterable (as before) *and* indexable:: >>> from enum...
2
by: Martin Engelhardt | last post by:
Hi, I want to use the following: Class Test ... Public Enum TestEnum as integer Enum1 = 128 Enum2 = 256 End Enum
4
by: =?Utf-8?B?0JrQvtC70LXQstCw?= | last post by:
I want to use an enum field in a constructor. For example: class Student { private string name; private enum university {.....}; private byte course; ......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.