Click here to Skip to main content
15,896,201 members
Articles / Programming Languages / C#

Recurring Date Generator with Pattern Coding

Rate me:
Please Sign up or sign in to vote.
4.95/5 (51 votes)
4 Sep 2007CPOL4 min read 147.1K   6.9K   98  
Create recurring dates using a user-defined pattern. Create recurring dates from a coded value that defines what the pattern should be.
using System;

namespace RecurrenceGenerator
{
    public enum EndDateType { NotDefined = -1, NoEndDate = 0, SpecificDate, NumberOfOccurrences };
    public abstract class RecurrenceSettings
    {
        public RecurrenceSettings(DateTime startDate)
        {
            this.startDate = startDate;
            endDateType = EndDateType.NoEndDate;
        }
        public RecurrenceSettings(DateTime startDate, DateTime endDate)
        {
            this.startDate = startDate;
            this.endDate = endDate;
            endDateType = EndDateType.SpecificDate;
        }
        public RecurrenceSettings(DateTime startDate, int numberOfOccurrences)
        {
            this.startDate = startDate;
            this.numberOfOccurrences = numberOfOccurrences;
            endDateType = EndDateType.NumberOfOccurrences;
        }

        DateTime? endDate; // Nullable date because there may or may not be an end date.
        DateTime startDate;
        int recurrenceInterval = 1;
        int numberOfOccurrences = 0;
        int regenerationInterval = 0;
        protected EndDateType  endDateType = EndDateType.NotDefined;

        internal abstract DateTime GetNextDate(DateTime currentDate);
        internal abstract RecurrenceValues GetValues();
        internal abstract RecurrenceValues GetValues(DateTime startDate, int numberOfOccurrences);
        internal abstract RecurrenceValues GetValues(DateTime startDate, DateTime endDate);

        /// <summary>
        ///     Get/Set the type of End Date. Occurrences, End Date, or no end date at all.
        /// </summary>
        /// <value>
        ///     <para>
        ///         EndDateType enumeration.
        ///     </para>
        /// </value>
        /// <remarks>
        ///     
        /// </remarks>
        public EndDateType TypeOfEndDate
        {
            get
            {
                return endDateType;
            }
            set
            {
                endDateType = value;
            }
        }

        /// <summary>
        /// Regenerate the Occurrence x-amount of days, weeks, etc. 
        /// after the current item is completed.
        /// </summary>
        public int RegenerationAfterCompletedInterval
        {
            get
            {
                return regenerationInterval;
            }
            set
            {
                regenerationInterval = value;
            }
        }

        public int NumberOfOccurrences
        {
            get
            {
                return numberOfOccurrences;
            }
            set
            {
                numberOfOccurrences = value;
            }
        }

        /// <summary>
        /// Readonly bool if this recurrence instance
        /// has an End date or note. If it does not then
        /// it means this instance has no ending date and should
        /// only create one IRecurrenceItem object.
        /// </summary>
        public bool HasEndDate
        {
            get
            {
                return endDate.HasValue;
            }
        }


        /// <summary>
        ///     End Date for the recurrence values.
        /// </summary>
        /// <value>
        ///     <para>
        ///         DateTime value.
        ///     </para>
        /// </value>
        /// <remarks>
        ///     
        /// </remarks>
        public DateTime? EndDate
        {
            get
            {
                if (endDate.HasValue)
                    return endDate.Value;
                else
                    return null;
            }
            set
            {
                endDate = value;
            }
        }

        /// <summary>
        ///     Start Date of the rucurence Values.
        /// </summary>
        /// <value>
        ///     <para>
        ///         DateTime value.
        ///     </para>
        /// </value>
        /// <remarks>
        ///     
        /// </remarks>
        public DateTime StartDate
        {
            get
            {
                return startDate;
            }
            set
            {
                startDate = value;
            }
        }

        /// <summary>
        /// This is the "Recurr every x-amount of Days, Weeks.
        /// </summary>
        public int RecurrenceInterval
        {
            get
            {
                return recurrenceInterval;
            }
            set
            {
                recurrenceInterval = value;
            }
        }

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) BOCA Software Technologies, Inc.
United States United States
.NET Developer in Garner, North Carolina. Specializing in WinForms development in C#, VB.Net.

CEO/Founder BOCA Software Technologies, Inc.

Comments and Discussions