0

i used the following code for enum printing i want to get the Sunday string in the messagebox with out changing the enum values. currently it gives OffDay

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            MessageBox.Show(Days.Sunday.ToString());
        }
    }

enum Days
{
    Sunday =0,
    OffDay = 0,
    Monday = 1,
    FirstWorkingDay = 1
}
Muhammad Nauman
  • 213
  • 1
  • 4
  • 20
  • MSDN: https://msdn.microsoft.com/en-us/library/a0h36syw%28v=vs.110%29.aspx "If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value." – Amir May 28 '15 at 04:58

3 Answers3

0

You must have unique number assigned to your Enum member to identify them uniquely as you have 0 for both Sunday and OffDay you are getting last one i.e. OffDay You can relate OffDay and FirstWorkingDay with Sunday and Monday respectively with Description attribute.

enum Days
{
    [Description("OffDay")]
    Sunday =0,
    [Description("FirstWorkingDay")]
    Monday = 2,       
}

Now you will get Sunday for MessageBox.Show(Days.Sunday.ToString());

To get the description attribute of enum members follow this Getting attributes of Enum's value post

Community
  • 1
  • 1
Adil
  • 146,340
  • 25
  • 209
  • 204
0

When two enumeration values have the same representation, there is no guarantee which is used by Enum.ToString. You can shorten the test to just Console.WriteLine(Language.Heb);. Since both Heb and heb are valid outputs, there are no guarantees which one you get. See Enum.ToString:

The above link states:

If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return. For example, the following enumeration defines two members, Shade.Gray and Shade.Grey, that have the same underlying value.

enum Shade
{
    White = 0, 
    Gray = 1, 
    Grey = 1, 
    Black = 2 
}

The following method call attempts to retrieve the name of a member of the Shade enumeration whose underlying value is 1. The method can return either "Gray" or "Grey", and your code should not make any assumptions about which string will be returned.

string shadeName = ((Shade) 1).ToString("F");
Peyman
  • 3,068
  • 1
  • 18
  • 32
Mohd Ahmed
  • 1,422
  • 13
  • 27
-1

you should use Unique value for each element, so if you want to use diffrent text You can add DescriptionAttribute and show that value:

public static string GetDescription(this object obj)
{
    var fi = obj.GetType().GetField(obj.ToString());
    var attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
    if (attributes.Length > 0)
        return attributes[0].Description;

    return obj.ToString();

}

And using like:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        MessageBox.Show(Days.OffDay.GetDescription());
    }
}

And Enum should be like this:

enum Days
{
   [Description("Sunday")]
   OffDay = 0,
   [Description("Monday")]
   FirstWorkingDay = 1
}
Peyman
  • 3,068
  • 1
  • 18
  • 32