Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
3.33/5 (2 votes)
Hi,

I use the following code:
C#
namespace wpfEmun
{
    enum Seizoen { lente , zomer, herfst, winter, autumn = herfst, spring = lente, summer = zomer };

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Seizoen seizoen = Seizoen.autumn;
            int seizoengetal = (int)seizoen;
            MessageBox.Show(string.Format("Seizoen nr {0} is {1}", seizoengetal, seizoen .ToString()));

        }




If I debug and add a watch to seizoen then I see "herfst", and the result is ok.
BUT, in the message box the text "autumn" is displayed.
So in short: var seizoen contains "herfst" but shows "autumn" and it should be "herfst".


Alex
Posted
Updated 5-Apr-14 8:04am
v2
Comments
Bernhard Hiller 7-Apr-14 2:56am    
If you are just interesetd in showing a "readable" value for your enum, look at a trick which uses a Description attribute for each enum value. If you need globalization/localization support, you can use the attribute for providing the name of the localized resource instead.

This is the perfectly expected result. I described the phenomenon you observed in detail in my article where I also created some interesting work-around solution: Enumeration Types do not Enumerate! Working around .NET and Language Limitations.

—SA
 
Share this answer
 
You should try it the other way around: now you do English word = Dutch word, but you should do Dutch word = English word:
C#
enum Seizoen { spring, summer, autumn, winter, lente = spring, zomer = summer, herfst = autumn };

First, add the English words to the enum and then add the Dutch words to it.
 
Share this answer
 
v3
Comments
Alex Houben 5-Apr-14 16:53pm    
Hi ProgramFOX,

Unfortunately this gives me 2 errors:
1) Cannot implicitly convert type 'wpfEmun.Form1.Seizoen' to 'int'. An explicit conversion exists (are you missing a cast?)
2) The evaluation of the constant value for 'wpfEmun.Form1.Seizoen.lente' involves a circular definition.

BTW I tested it on VS2008 & VS2012 but they give both the same result.
Thomas Daniels 6-Apr-14 4:05am    
I updated my answer. I was wrong about the order of items in the enum.
Sergey Alexandrovich Kryukov 5-Apr-14 18:10pm    
This is not a solution. The problem is quite non-trivial. Please see my answer where I refer to my detailed article on this problem (the article is about the fact that enum types don't support IEnumerable interface, but big part is devoted to this very problem).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900