Reputation: 91
I have my own DaysOfWeek Flag enum (kind of https://learn.microsoft.com/en-us/previous-versions/ms886541(v=msdn.10))
[Flags]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 4,
Thursday = 8,
Friday = 16,
Saturday = 32
}
And I need to compare standard DayOfWeek with mine. How can I do that?
Upvotes: 3
Views: 2242
Reputation: 1
public enum BitwiseDayOfWeek
{
Sunday = 1,
Monday = 2,
Tuesday = 4,
Wednesday = 8,
Thursday = 16,
Friday = 32,
Saturday = 64
}
public class Program
{
public static void Main(string[] args)
{
BitwiseDayOfWeek scheduledDayOfWeek
= BitwiseDayOfWeek.Saturday | BitwiseDayOfWeek.Sunday;
// turn System.DayOfWeek (DateTime.Now.DayOfWeek) into BitwiseDayOfWeek
BitwiseDayOfWeek currentDayOfWeek
= (BitwiseDayOfWeek)Math.Pow(2, (double)DateTime.Now.DayOfWeek);
// test if today is the scheduled day
if ( (currentDayOfWeek & scheduledDayOfWeek) == currentDayOfWeek )
Console.WriteLine(currentDayOfWeek);
Console.WriteLine("---------------------");
Console.ReadLine();
}
}
Upvotes: 0
Reputation: 91
Thanks everyone for help. Finally, I have solution
Own DaysOfWeek with flag:
[Flags]
public enum DaysOfWeek
{
None = 0,
Sunday = 1 << 0,
Monday = 1 << 1,
Tuesday = 1 << 2,
Wednesday = 1 << 3,
Thursday = 1 << 4,
Friday = 1 << 5,
Saturday = 1 << 6,
}
Since own enum has same order of days, we can write extension method to convert standard DayOfWeek to own
public static class EnumExtensions
{
public static DaysOfWeek ToFlag(this DayOfWeek dayOfWeek)
{
var mask = 1 << (int)dayOfWeek;
return (DaysOfWeek)Enum.ToObject(typeof(DaysOfWeek), mask);
}
}
And usage:
var days = DaysOfWeek.Sunday | DaysOfWeek.Friday;
var day = DayOfWeek.Sunday;
var ownDay = day.ToFlag();
if (days.HasFlag(ownDay))
// Do whatever;
playground: https://dotnetfiddle.net/sV3yge
Upvotes: 3
Reputation: 1307
If you change your Enum like this:
[Flags]
public enum DayOfWeek
{
Sunday = 0,
Monday = 1,
Tuesday = 2,
Wednesday = 3,
Thursday = 4,
Friday = 5,
Saturday = 6
}
You can try this:
class Program
{
public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)
{
int mineInt = (int)mine;
int cSharpInt = (int)cSharp;
return mineInt == cSharpInt;
}
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
DayOfWeek dayOfWeek = DayOfWeek.Sunday;
bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}
If you can't change your Enum, you can try this:
class Program
{
public static bool Equal(DayOfWeek mine, System.DayOfWeek cSharp)
{
if (mine == DayOfWeek.Friday && cSharp == System.DayOfWeek.Friday ||
mine == DayOfWeek.Monday && cSharp == System.DayOfWeek.Monday ||
mine == DayOfWeek.Saturday && cSharp == System.DayOfWeek.Saturday ||
mine == DayOfWeek.Sunday && cSharp == System.DayOfWeek.Sunday ||
mine == DayOfWeek.Thursday && cSharp == System.DayOfWeek.Thursday ||
mine == DayOfWeek.Tuesday && cSharp == System.DayOfWeek.Tuesday ||
mine == DayOfWeek.Wednesday && cSharp == System.DayOfWeek.Wednesday)
return true;
return false;
}
static void Main(string[] args)
{
DateTime dateTime = DateTime.Now;
DayOfWeek dayOfWeek = DayOfWeek.Tuesday;
bool areEqual = Equal(dayOfWeek, dateTime.DayOfWeek);
Console.WriteLine(areEqual);
Console.ReadKey();
}
}
Upvotes: -3
Reputation: 35380
Since your enum uses the same order of days as the built-in DayOfWeek
, all you need to do is to use the variable of DayOfWeek
type as the exponent of 2
and then bitwise-AND it with your enum variable.
Something like this (this will check if Monday
flag of your enum is 1
or not):
MyDayOfWeek Days = MyDayOfWeek.Monday | MyDayOfWeek.Friday;
DayOfWeek D = DayOfWeek.Monday;
var Mask = 1 << (int)D;
if ((Mask & (int)Days) == Mask)
//Do whatever;
I have renamed your enum to MyDaysOfWeek
, whereas DayOfWeek
is the built-in .NET type. You can do the same for any day of week.
As @HenkHolterman pointed out (thanks for that), you'll have problems with your Sunday
set to 0
. A Flags
enum should normally start with a member named None
that is equal to 0
and which indicates that none of the flags of the variable are set.
Upvotes: 3