-1

I have an enum value that represent a range of week days (e.g. 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.

Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100
Assaf Our
  • 611
  • 3
  • 9
  • 25
  • 3
    You should take a look at the [Flag attribute](http://stackoverflow.com/questions/8447/what-does-the-flags-enum-attribute-mean-in-c). – krillgar Jan 30 '17 at 11:58
  • 1
    You will have to make your enum independent of `DayOfWeek` since the latter uses incremental values that will not wirk for flag enums. – poke Jan 30 '17 at 12:01
  • @krillgar , if I understand it correct ,using the flag will not make the system to get the value of (E.G. Tuesday) in a query .just a string . – Assaf Our Jan 30 '17 at 12:05
  • Why would you make an enum and assign its values by another enum, why don't you use the DayOfWeek itself? – Ashkan Mobayen Khiabani Jan 30 '17 at 12:06
  • Is it becuase the first day in your week is not sunday? its saturday may be? – Ashkan Mobayen Khiabani Jan 30 '17 at 12:08
  • 3
    I think your major problem is to use one type (enum) for both single and multiple values. Your sum of `DayOfWeek`s is in fact just an integer and if you do this for more 'lists' of days, you won't be able to get the list back, since different lists can summate to the same integers. Flags can solve this problem, but only if you define the enum properly and do not summate, but bitwise operate on the enum. – Lukas Körfer Jan 30 '17 at 12:09
  • 2
    `DayOfWeek.Monday = 1`, `DayOfWeek.Tuesday = 2` and `DayOfWeek.Wednesday = 3`. With your current logic `DayOfWeek.Monday + DayOfWeek.Tuesday == DayOfWeek.Wednesday`. – Manfred Radlwimmer Jan 30 '17 at 12:12
  • can I understand from all the comments that I get that I'm not using doable approach and I should try to find Diff solution ? – Assaf Our Jan 30 '17 at 12:17
  • You need to use the `[Flag]` attribute, and set the values of each value in your enum to a multiple of 2. (You're creating binary values for each value.) Then that avoids the situation outlined in Manfred's comment. – krillgar Jan 30 '17 at 12:19
  • @ManfredRadlwimmer dayofweek in system is an int , until you say so I was sure it comes from system.datatime with a string value , well one more mistake by me. – Assaf Our Jan 30 '17 at 12:20
  • How does your "Friendly string" `SunTilFri` look? Do you just return all the days in the span? – Magnus Jan 30 '17 at 12:22
  • @Magnus , its address to Resources (as part of.Globalization) and take just a string from my Db Table – Assaf Our Jan 30 '17 at 12:25
  • @krillgar can you please explain a bit more – Assaf Our Jan 30 '17 at 12:31
  • 1
    @AssafOur Take a look at Wheels73's answer. Because there's already an existing enum, his solution would work better. – krillgar Jan 30 '17 at 13:01

1 Answers1

3

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.

Wheels73
  • 2,850
  • 1
  • 11
  • 20