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:
If you need an enumeration then you have to declare an enumeration. You don't convert anything to an enum type at run time, any more than you convert anything to a class or structure. If you need such an enumeration then your second code snippet is what you need to use.

Of course, the System.DayOfWeek enumeration already exists, so there would be no need to declare your own.

If what you're saying is that you have an enumeration and you have a list of strings containing the names of values from that enumeration and you want to convert between the two then that's different. The Enum.Parse method exists for that, e.g.
C#:
var dayNames = new List<string> {"Monday", "Tuesday ", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
var dayValues = dayNames.Select(s => (DayOfWeek) Enum.Parse(typeof(DayOfWeek), s)).ToList();
or, without LINQ:
C#:
var dayNames = new List<string> {"Monday", "Tuesday ", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
var dayValues = new List<DayOfWeek>();

foreach (var dayName in dayNames)
{
    var dayValue = (DayOfWeek) Enum.Parse(typeof(DayOfWeek), dayName);

    dayValues.Add(dayValue);
}
 
As a quick aside, the old WinAPI and VB6 style of using Hungarian notation for identfiers is discouraged in the .NET world. So lstDays should simply be days.
 
Use the Enum DayOfWeek from the system namespace. And then create a List<T> - such as a list of day of week : List<DayOfWeek> Try something like this :
C#:
            List<DayOfWeek> lDayOfWeek = new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
                DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday };
            foreach (Day day in lDayOfWeek)
            {
                if (day == Day.Monday || day == Day.Friday)
                    Debug.WriteLine($"It's {day}");

                if (day >= Day.Monday && day <= Day.Friday)
                    Debug.WriteLine($"{day} is a weekday");
                else
                    Debug.WriteLine($"{day} is a weekend");
            }
Output :
C#:
It's Monday
Monday is a weekday
Tuesday is a weekday
Wednesday is a weekday
Thursday is a weekday
It's Friday
Friday is a weekday
Saturday is a weekend
Sunday is a weekend
You can then do comparisons using comparison operators on the day of the DayOfWeek. If you want Monday to be your first day of the week, then start your List<DayOfWeek> with Sunday being the first day, like I've done. If you want Sunday to be your first day, then have Saturday as your first entry in your list instead.

Hope this helps.
 
You can then do comparisons using comparison operators on the day of the DayOfWeek. If you want Monday to be your first day of the week, then start your List<DayOfWeek> with Sunday being the first day, like I've done. If you want Sunday to be your first day, then have Saturday as your first entry in your list instead.
Doesn't work. I got the following output:
C#:
FirstDayIsMonday
Sunday is a weekend
It's Monday
Monday is a weekday
Tuesday is a weekday
Wednesday is a weekday
Thursday is a weekday
It's Friday
Friday is a weekday
Saturday is a weekend

FirstDayIsSunday
Saturday is a weekend
Sunday is a weekend
It's Monday
Monday is a weekday
Tuesday is a weekday
Wednesday is a weekday
Thursday is a weekday
It's Friday
Friday is a weekday

using the following code as per the description in the quote above:
Arranged the list as described in quoted text.:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection.PortableExecutable;
using System.Threading;

namespace SimpleCS
{
    class Program
    {
        void ShowDays(List<DayOfWeek> daysOfWeek)
        {
            foreach (DayOfWeek day in daysOfWeek)
            {
                if (day == DayOfWeek.Monday || day == DayOfWeek.Friday)
                    Console.WriteLine($"It's {day}");

                if (day >= DayOfWeek.Monday && day <= DayOfWeek.Friday)
                    Console.WriteLine($"{day} is a weekday");
                else
                    Console.WriteLine($"{day} is a weekend");
            }
            Console.WriteLine();
        }

        void FirstDayIsMonday()
        {
            Console.WriteLine(nameof(FirstDayIsMonday));
            List<DayOfWeek> daysOfWeek = new List<DayOfWeek> { DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
                DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday, DayOfWeek.Saturday };

            ShowDays(daysOfWeek);
        }

        void FirstDayIsSunday()
        {
            Console.WriteLine(nameof(FirstDayIsSunday));
            List<DayOfWeek> daysOfWeek = new List<DayOfWeek> { DayOfWeek.Saturday, DayOfWeek.Sunday, DayOfWeek.Monday,
                    DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday };

            ShowDays(daysOfWeek);
        }

        void Run()
        {
            FirstDayIsMonday();
            FirstDayIsSunday();
        }

        static void Main(string[] args)
        {
            new Program().Run();
        }
    }
}
 
Just to clarify something, it doesn't matter that I am using Day and DaysOfWeek unless you are using the numeric values of the enums for the days. But since you appear to be looking for strings of the days, then this is just another way to do it.

Docs for Day : Day Enum (System.Windows.Forms)

I don't know what you done in your code, but as I wrote it, it works fine for me.
 
That's because enum Day is not the same as enum DayOfWeek
C#:
enum Day
{
    Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
}

enum DayOfWeek
{
    Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday
}

So the value DayOfWeek.Sunday is getting mapped to Day.Monday.


 
That's because enum Day is not the same as enum DayOfWeek
I already pointed that out.

Second. The order of how the enums are inserted in the list determines how they are iterated. You will notice the first day gets skipped until last. I wouldn't be bothered to change it unless you need to use the numbers of the enums for something. They are comparing by name not by index.
 
And changing the code in post #4 to consistently use Day instead of DayOfWeek:
Consistent use of Day instead of DayOfWeek:
List<Day> lDayOfWeek = new List<Day> { Day.Sunday, Day.Monday, Day.Tuesday,
                                      Day.Wednesday, Day.Thursday, Day.Friday, Day.Saturday };
foreach (Day day in lDayOfWeek)
{
    if (day == Day.Monday || day == Day.Friday)
        Debug.WriteLine($"It's {day}");

    if (day >= Day.Monday && day <= Day.Friday)
        Debug.WriteLine($"{day} is a weekday");
    else
        Debug.WriteLine($"{day} is a weekend");
}

produces the following output:
C#:
Sunday is a weekend
It's Monday
Monday is a weekday
Tuesday is a weekday
Wednesday is a weekday
Thursday is a weekday
It's Friday
Friday is a weekday
Saturday is a weekend
 
You're arguing semantics :
C#:
            List<Day> lDayOfWeek = new List<Day> { Day.Wednesday, Day.Thursday, Day.Friday,
                Day.Saturday, Day.Sunday, Day.Monday, Day.Tuesday };
            foreach (Day day in lDayOfWeek)
            {
                if (day == Day.Monday || day == Day.Friday)
                    Debug.WriteLine($"It's {day}");

                if (day >= Day.Monday && day <= Day.Friday)
                    Debug.WriteLine($"{day} is a weekday");
                else
                    Debug.WriteLine($"{day} is a weekend");
            }
Output :
C#:
Wednesday is a weekday
Thursday is a weekday
It's Friday
Friday is a weekday
Saturday is a weekend
Sunday is a weekend
It's Monday
Monday is a weekday
Tuesday is a weekday
 
Lol What is important is the order in which you put them in the list. Day can still be mapped to the Day of DaysOfWeek.

I'd only change to the way you done it above if I was using a sorted list. ;)
 
Yes, but notice in post #12 that "Wednesday" was not skipped, but in post #4 you were saying that the first item in the list: "Sunday" would be skipped.
 
Yes because I changed :
C#:
List<DayOfWeek> lDayOfWeek = new List<DayOfWeek>
To :
C#:
List<Day> lDayOfWeek = new List<Day>
Use my original code and it will skip the first entry and call it last.
 
Back
Top Bottom