4

I have the enum:

    [Flags, Serializable,]
    public enum WeekDays {
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Saturday = 64,
        WeekendDays = Sunday | Saturday,
        WorkDays = Monday | Tuesday | Wednesday | Thursday | Friday,
        EveryDay = WeekendDays | WorkDays
    }

And, I have property WeekDays in a class that contain value of WeekDays enum:

public int WeekDays {get; set;}

For example WorkDays contain 62( from Monday to Friday).
How to check that current WeekDays property contain current day?

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
user348173
  • 8,818
  • 18
  • 66
  • 102

7 Answers7

9

Enum has method HasFlag to determine whether one or more bit fields are set in the current instance.

Kirill Polishchuk
  • 54,804
  • 11
  • 122
  • 125
  • Apparently there are performance issues - stated in the MSDN community feedback portion of your link. Link to a faster implementation: http://stackoverflow.com/q/7984377/328397 However, as always, slow performance isn't an issue until you make it one. – Adam Houldsworth Feb 23 '12 at 10:58
6

Use the bitwise operator & to see if a value is part of a set:

var today = WeekDays.Thursday;
var workdays = WeekDays.WorkDays;

if((today & workdays) == today) {
    // today is a workday
}

if((today & WeekDays.Friday) == today) {
    // it's friday
}
Jon
  • 428,835
  • 81
  • 738
  • 806
  • +1 Further reading: http://journal.stuffwithstuff.com/2008/03/05/checking-flags-in-c-enums/ – Adam Houldsworth Feb 23 '12 at 10:54
  • 1
    @Chris The sad thing is, once you start talking to date logic you do actually get code that only works on Thursdays in the wild... In fact I once remember seeing maintenance code that needed to run "periodically" every 4 years put onto the 29th Feb, lord only knows what it was doing and what they were thinking. – Adam Houldsworth Feb 23 '12 at 11:03
  • 1
    @AdamHouldsworth: I wish I could say that surprised me... ;-) – Chris Feb 23 '12 at 11:05
3

Use &:

    [Flags, Serializable,]
    public enum WeekDays
    {
        Sunday = 1,
        Monday = 2,
        Tuesday = 4,
        Wednesday = 8,
        Thursday = 16,
        Friday = 32,
        Saturday = 64,
        WeekendDays = Sunday | Saturday,
        WorkDays = Monday | Tuesday | Wednesday | Thursday | Friday,
        EveryDay = WeekendDays | WorkDays
    }

    public static WeekDays Days { get; set; }

    private static void Main(string[] args)
    {
        WeekDays today = WeekDays.Sunday;

        Days = WeekDays.WorkDays;

        if ((Days & today) == today)
        {
            Console.WriteLine("Today is included in Days");
        }

        Console.ReadKey();
    }
ken2k
  • 48,145
  • 10
  • 116
  • 176
3
WeekDays weekDayValue = .... ;
var today = Enum.Parse(typeof(WeekDays),DateTime.Now.DayOfWeek.ToString())
bool matches = ( weekDayValue & today ) == today;
wiero
  • 2,176
  • 1
  • 19
  • 28
  • This won't work since dayOfWeek is from 0-6 which doesn't not match the values in this enum. – Chris Feb 23 '12 at 11:04
  • DayOfWeek is an enum and have values which match values in WeekDays http://msdn.microsoft.com/en-us/library/system.dayofweek.aspx – wiero Feb 23 '12 at 11:06
  • I agree its an enum but the values are not the same as those in the above defined enum. The above enum is defining Thursday as 16 for example. What is the value of DayOfWeek for thursday? Unless I'm missing something its 4... – Chris Feb 23 '12 at 11:27
  • i use DateTime.Now.DayOfWeek.ToString(); DayOfWeek.Sunday.ToString() == "Sunday"; Enum.Parse(typeof(WeekDays),"Sunday") == WeekDays.Sunday – wiero Feb 23 '12 at 11:33
  • Ah, yes. I see now. Sorry. I hadn't twigged that the .ToString would be returning the Enum name instead of the underlying value. Thanks for clearing up my confusion and apologies for my false allegations. ;-) (And +1 for a nicer way of getting today than I used). – Chris Feb 23 '12 at 11:37
1

do:

int r = (int)(WeekDays.WorkDays & WeekDays.Sunday)
if (r !=0)
  you have it.
Saeed Amiri
  • 22,252
  • 5
  • 45
  • 83
1

You just need to use bitwise boolean logic to do this. if you were to say

WeekDays.Tuesday & WeekDays.WorkDays

then the boolean logic would return 4 (since 4&62 = 4).

Essentially the & is saying that for each bit position if both are 1 then return that number. So you then just need to check if it is > 0.

To get Today in your current format then you'll need to do a bit of maths...

(int)DateTime.Now.DayOfWeek will return the day of the week ranging from 0 (sunday) to 6 (saturday).

We need to map these to match your range. Fortunately this is easy to do with Math.Pow.

int today = Math.Pow(2,(int)DateTime.Now.DayOfWeek);
if (today&WeekDays.WorkDays>0)
    isWorkDay = true;
else
    isWorkDay = false;
Chris
  • 27,210
  • 6
  • 71
  • 92
0

Get all enum names and values, then perform 'and' calculation to test if WeekDays contains current day

linquize
  • 19,828
  • 10
  • 59
  • 83