Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
65
Pattern Days of the Week
posted

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.

Parents
  • 69832
    Verified Answer
    Offline posted

     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;
        }

        return retVal;
    }

Reply Children
No Data