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?

2
  • 5
    If you want your property to be of the enum type it should be public WeekDays WeekDays... just sayin'. Commented Feb 23, 2012 at 10:50
  • Test whether x contains some subset of flags: (x & subset) == subset Commented Feb 23, 2012 at 10:52

7 Answers 7

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

Apparently there are performance issues - stated in the MSDN community feedback portion of your link. Link to a faster implementation: stackoverflow.com/q/7984377/328397 However, as always, slow performance isn't an issue until you make it one.
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 } 

3 Comments

@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.
@AdamHouldsworth: I wish I could say that surprised me... ;-)
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(); } 

Comments

3
WeekDays weekDayValue = .... ; var today = Enum.Parse(typeof(WeekDays),DateTime.Now.DayOfWeek.ToString()) bool matches = ( weekDayValue & today ) == today; 

5 Comments

This won't work since dayOfWeek is from 0-6 which doesn't not match the values in this enum.
DayOfWeek is an enum and have values which match values in WeekDays msdn.microsoft.com/en-us/library/system.dayofweek.aspx
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...
i use DateTime.Now.DayOfWeek.ToString(); DayOfWeek.Sunday.ToString() == "Sunday"; Enum.Parse(typeof(WeekDays),"Sunday") == WeekDays.Sunday
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).
1

do:

int r = (int)(WeekDays.WorkDays & WeekDays.Sunday) if (r !=0) you have it. 

Comments

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; 

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.