I am using the recurring appointments in the WinSchedule. I have appointments that happen on Mondays and Wednesdays. Currently, I am adding these value together(2 + 8) = 10 and storing 10 in a database. I was wondering if this will work when I go to load these into a series of checkboxes. Is there way to load these values with some kind of division algorith, so that I know that if I have a value of 10 that is always Monday and Wednesday? Thanks for any input.
What you refer to here is typically called bit screening. The values in the RecurrencePatternDaysOfWeek enumeration were made to be the value you get when you raise the number two to the exponential power of the corresponding day of the week. The following code sample demonstrates how to screen the bits in both directions:
private void button1_Click(object sender, EventArgs e){ RecurrencePatternDaysOfWeek value = RecurrencePatternDaysOfWeek.Sunday | RecurrencePatternDaysOfWeek.Thursday | RecurrencePatternDaysOfWeek.Saturday; List<System.DayOfWeek> daysOfWeek = this.GetDaysOfWeek( value ); value = this.GetPatternDaysOfWeek( daysOfWeek );}
private List<System.DayOfWeek> GetDaysOfWeek( RecurrencePatternDaysOfWeek patternDaysOfWeek ){ List<System.DayOfWeek> retVal = new List<System.DayOfWeek>();
// Iterate from 1 to 7 and raise 2 to the power of the loop value, // then screen the resulting bit against the specified patternDaysOfWeek, // so that each day of the week is checked. for ( int i = 0; i < 7; i ++ ) { int bitScreen = (int)Math.Pow( 2f, i );
if ( ( (int)patternDaysOfWeek & bitScreen) == bitScreen ) retVal.Add( (System.DayOfWeek)i ); }
return retVal;}
private RecurrencePatternDaysOfWeek GetPatternDaysOfWeek( List<System.DayOfWeek> daysOfWeek ){ // Start with a value of 'None', which is equal to 0x0000 RecurrencePatternDaysOfWeek retVal = RecurrencePatternDaysOfWeek.None;
// Iterate the list of days of the week; for each one // that is present in the list, derive the RecurrencePatternDaysOfWeek // value using the formula (2 ^ [day of week]), and then set that bit // in the value to be returned. for ( int i = 0; i < daysOfWeek.Count; i ++ ) { System.DayOfWeek dayOfWeek = daysOfWeek[i]; int intval = (int)Math.Pow(2, (int)dayOfWeek );
retVal |= (RecurrencePatternDaysOfWeek)intval; }
Hi, Objective : To Find the Current Day falls in Recurrence Pattern
I have used the following to code to find the current day has recurrence or not. Here, In CheckDayOfWeekMaskUtc function, I pass the variables as,
iValue = Value of DayOfWeekMaskUtc of the Recurrence
iApptValue = Value of Current Date (Day of the week)
Is this Method Correct ? Please let me know if any changes......
Private Function CheckDayOfWeekMaskUtc(ByVal iValue As Integer, ByVal iApptValue As Integer) As Boolean CheckDayOfWeekMaskUtc = False Dim iRes As Integer = 1 Dim iTemp As Integer = 1 For i As Integer = 6 To 0 Step -1 iRes = 2 ^ i If iValue >= iRes Then iValue = iValue - iRes iTemp = SetDay(iRes) If iTemp.Equals(iApptValue) Then CheckDayOfWeekMaskUtc = True Exit Function End If End If Next End Function Private Function SetDay(ByVal iDay As Integer) As Integer Select Case iDay Case 1 SetDay = DayOfWeek.Sunday Case 2 SetDay = DayOfWeek.Monday Case 4 SetDay = DayOfWeek.Tuesday Case 8 SetDay = DayOfWeek.Wednesday Case 16 SetDay = DayOfWeek.Thursday Case 32 SetDay = DayOfWeek.Friday Case 64 SetDay = DayOfWeek.Saturday Case Else SetDay = iDay End Select End Function
Regards,
Nagendran