Resolved How to Convert List<string> values to Enum

Anburaj Marikkani

New member
Joined
Jul 29, 2020
Messages
2
Programming Experience
3-5
I try to Covert the List<> value to Enum but i'm Failed.

List<string> lstDays = new List<string> { "Monday", "Tuesday ", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };

Need Enum like

C#:
enum lstDays
    {
        Monday,
        Tuesday,
        Wednesday,
        Thursday,
        Friday,
        Saturday,
        Sunday
    }

Please can help?
 
Last edited by a moderator:
Ahh so this is interesting. As I wrote this out, I assumed there was something under the hood causing the first item to be skipped, but it was not skipped. In fact, it appears to be a bug in Microsoft's docs???

Let me explain. In DayOfWeek from system.dayofweek the index of the enum starts with its day starting on Sunday which its index is 0 :
C#:
[System.Runtime.InteropServices.ComVisible(true)]
    public enum DayOfWeek {
        Sunday = 0,
Whereas Day from System.Windows.Forms does not hold the same index and it starts on a Monday giving the illusion it was skipping the first item :
C#:
    public enum Day {
        Monday = 0,
Reference for Day : Reference Source
Reference for DayOfWeek : Reference Source

As you can see, this is distinctly different. Whereas I blindly assumed Microsoft would have only ever defined Day as an enum once in the framework. And I also assumed that DayOfWeek would also have been some inherited class or struct for the Day enum. Initially you'd think; well i did, and so I thought that the enums would have been referred to only once. But having two enums technically referring to the same thing which are days of the week, should be holding the same index and starting on the same day, so that the iteration of both DaysOfWeek and Day would be the same. This will only happen if the week starts on the same day which they do not. I'm considering submitting this as a bug, or at best declare it defective or contradictory code. Why? Well unless you look under the hood at the reference docs which I normally do, you would never know why my original code appeared to work. I'm attaching some code samples below to compare the output of all three variations. :
C#:
            Debug.WriteLine("......... Checking DayOfWeek ..........");
            List<DayOfWeek> lDayOfWeek = new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
                DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday };
            foreach (DayOfWeek day in lDayOfWeek)
            {
                if (day >= DayOfWeek.Monday && day <= DayOfWeek.Friday)
                    Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
                else
                    Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
            }

            Debug.WriteLine("......... Checking Day ..........");

            List<Day> lDay = new List<Day> { Day.Sunday, Day.Monday, Day.Tuesday,
                Day.Wednesday, Day.Thursday, Day.Friday, Day.Saturday };
            foreach (Day day in lDay)
            {
                if (day >= Day.Monday && day <= Day.Friday)
                    Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
                else
                    Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
            }

            Debug.WriteLine("......... Checking Day in DaysOfWeek [Causes incorrect results]..........");

            List<DayOfWeek> DOfWeek = new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
                DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday };
            foreach (Day day in DOfWeek)
            {
                if (day >= Day.Monday && day <= Day.Friday)
                    Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
                else
                    Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
            }
It was only when Skydiver pointed out above p/#9 that the order of the enums set up the values, and it was this that got me thinking to check the references to see how it was actually operating. Moral of the story, write it as Skydiver has done on p/#11 or use one of my first or second variation above and don't use DayOfWeek with Day because they are both declared to start on different days. Sunday and Monday, both starting indexes are zero for both days.

If anyone can explain to me why these emums are setup to use different days and why one of them uses a default value, I'd love to know.

Edit fixed broken code.
 
Last edited:
Here's the output the above code produces :
C#:
......... Checking DayOfWeek ..........
Sunday is a weekend - indexed 0
Monday is a weekday - indexed 1
Tuesday is a weekday - indexed 2
Wednesday is a weekday - indexed 3
Thursday is a weekday - indexed 4
Friday is a weekday - indexed 5
Saturday is a weekend - indexed 6
......... Checking Day ..........
Sunday is a weekend - indexed 6
Monday is a weekday - indexed 0
Tuesday is a weekday - indexed 1
Wednesday is a weekday - indexed 2
Thursday is a weekday - indexed 3
Friday is a weekday - indexed 4
Saturday is a weekend - indexed 5
......... Checking Day in DaysOfWeek [Causes incorrect results]..........
Monday is a weekday 0
Tuesday is a weekday 1
Wednesday is a weekday 2
Thursday is a weekday 3
Friday is a weekday 4
Saturday is a weekend - indexed 5
Sunday is a weekend - indexed 6
 
don't use DayOfWeek with Day
No, why would anyone do that? :) They are different enums containing different values.
If anyone can explain to me why these emums are setup to use different days and why one of them uses a default value, I'd love to know.
As explained in Day enum it is used by MonthCalendar control (both System.Windows.Forms). The Default value is used by MonthCalendar.FirstDayOfWeek Property (System.Windows.Forms) with DefaultValue attribute. The control does this to get system default, it also listens for SystemEvents.UserPreferenceChanged to see changes in UserPreferenceCategory.Locale.
 
I wish I could recall my full conversation with Michael Kaplan many years ago when we used to both work at the Evil Empire, and I'd asked about the two enums. Alas, both of us left the company, and Michael has also left this plane of existence. (Rest in Peace, my friend.) Part of the explanation he had given me is as @JohnH had mentioned above regarding the use of the Day enum for use with the MonthCalendar. But there was more to it because Microsoft's tendency is to let Default to either be 0, or -1. But in this case Day.Default is 7 rather the expected 0 or -1. There was something about choosing Monday to be 0 which made internationalization easier, as well as with the actual implementation of any kind of calendar, but I don't recall it right now.
 
I'm going to have to read up on what you're throwing at me John. I thought it very odd to have two enums declared in the framework which both of them start on different days causing the indexes to be different. I'm sorry, but if you agree with this, you will need to explain in detail to me so I can understand the reasoning for it.

It's my opinion that if you're going to write a framework, you should declare items once and not change indexes of those enums by starting the days on a completely different day to other enums declared for the same purpose-built structure. And if you need to change values, that's why we use things like classes, structs and overrides etc. Have you looked at the reference "under the hood" links John?

And whats with the Day Checker starting at 6?
......... Checking Day ..........
Sunday is a weekend - indexed 6
 
And whats with the Day Checker starting at 6?
If you're incorrectly mixing and comparing these values you'll end up with converting the integer value of one enum to the corresponding value of the other enum.
 
Hmm, in that instance I didn't mix day with DayOfWeek :
C#:
            Debug.WriteLine("......... Checking Day ..........");

            List<Day> lDay = new List<Day> { Day.Sunday, Day.Monday, Day.Tuesday,
                Day.Wednesday, Day.Thursday, Day.Friday, Day.Saturday };
            foreach (Day day in lDay)
            {
                if (day >= Day.Monday && day <= Day.Friday)
                    Debug.WriteLine($"{day} is a weekday - indexed {(int)day}");
                else
                    Debug.WriteLine($"{day} is a weekend - indexed {(int)day}");
            }
What gives?
 
What gives?
In this code you are using Day enum only. Your list starts with Day.Sunday and 6 is the integer value of that value.
 
That the one, thanks. I still don't agree with the reasoning for creating additional declarations for enums so one is on a different day just to suit the devs who made the calendar control. . .

Granted, I accept they are also in different namespaces, and I should have been more diligent in researching before assuming and using as they say... they should recommend not mixing these two enums by adding comments to the source code so we can read it in the editor. ?

Seems senseless all because they wanted to build a control and give it an additional value, so lets start this enum on a different day to the standard in the system namespace. Like that's not going to confuse anyone. Right? Lol Well it caught me
 
I think I found it. ISO 8601 and RFC3339 declare Monday, not Sunday to be the first day of the week.
 
You sure? If you're to go by the RFC, then Sunday should never have been set as the first day of the week in DayOfWeek. Doing so puts Sunday on a zero index. Whereas Day is Monday with a zero index.

Setting the enums like this, It was really silly in my opinion. Haven not been provoked to look, I'd never have known/expected they be different.
 
I was looking at Appendix A of the same link:
C#:
date-wday       =  DIGIT  ; 1-7  ; 1 is Monday, 7 is Sunday

This is to allows writing out at date as "2020-W31-3"
 
date-wday = DIGIT ; 1-7 ; 1 is Monday, 7 is Sunday
So Day enum has the order right. They should probably have put Default first according to Enum Design - Framework Design Guidelines
✔ DO provide a value of zero on simple enums.

Consider calling the value something like "None." If such a value is not appropriate for this particular enum, the most common default value for the enum should be assigned the underlying value of zero.
Although the int value for enums is mostly meaningless, it is just a way to arrange them, and for flags enums to be combined easily as bits.

I keep thinking of how annoying it is to move Sunday last in DayOfWeek enum :mad:
 
And Wikipedia claims that ISO 8601 declared it as the beginning of the week.
While, for example, the United States, Canada, Brazil, Japan and other countries consider Sunday as the first day of the week, and while the week begins with Saturday in much of the Middle East, the international ISO 8601 standard[a] has Monday as the first day of the week. The ISO standard includes the ISO week date system, a numbering system for weeks within a given year, where each week starting on a Monday is associated with the year that contains that week's Thursday (so that if a year starts in a long weekend Friday–Sunday, week number one of the year will start after that). ISO 8601 assigns numbers to the days of the week, running from 1 to 7 for Monday through to Sunday.

An ISO 8601 week
 
Back
Top Bottom