I have an enum value that represent a range of week days (eg Sunday until Friday) that I present to user in a friendly name.
I tried to give it the value
(SunTilTHur = DayOfWeek.Sunday + DayOfWeek.Monday + DayOfWeek.Tuesday)
but I'm getting the error
The switch statement contains multiple cases with the label value
The code I tried:
public MyDayOfWeek Days { get; set; }
public string DaysFriendlyName => this.Days.ToFriendlyName();`
public enum MyDayOfWeek
{
Sunday = DayOfWeek.Sunday,
Monday = DayOfWeek.Monday,
// ..
SunTilFir = DayOfWeek.Sunday + DayOfWeek.Monday + DayOfWeek.Tuesday,//+...
public static string ToFriendlyName(this MyDayOfWeek days)
{
switch (days)
{
case MyDayOfWeek.SunTilFir:
return @Resources.FormResources.SunTilFri;
Is it possible to set this values and how? Thanks in advance.
There's no point in wrapping the DayOfWeek enum in an enum. I would create your own helper class and add the methods you need to satisfy the range of checks. I've used a similar approach to the below when I needed to know when a day was a working day or weekend etc for a payments system... which seemed to work.
public static class MyDayHelper
{
public static bool IsWeekDay(DayOfWeek myday)
{
return myday >= DayOfWeek.Monday && myday <= DayOfWeek.Friday;
}
public static bool IsWeekEnd(DayOfWeek myday)
{
return myday == DayOfWeek.Saturday || myday == DayOfWeek.Sunday;
}
public static string ReturnWeekdays()
{
return string.Format("{0},{1},{2},{3},{4}", DayOfWeek.Monday, DayOfWeek.Tuesday,
DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday);
}
public static string SunTilFri
{
get
{
return string.Format("{0},{1},{2},{3},{4},{5}", DayOfWeek.Sunday, DayOfWeek.Monday, DayOfWeek.Tuesday,
DayOfWeek.Wednesday, DayOfWeek.Thursday, DayOfWeek.Friday);
}
}
}
You can obviously add the relevant methods for you...then you can test the conditions based upon what your current day is
var myDay = DateTime.Now.DayOfWeek;
if (MyDayHelper.IsWeekDay(myDay))
{
//Do something
}
if (MyDayHelper.IsWeekEnd(myDay))
{
//Do something
}
Hope that helps you come up with a solution.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.