Back to Devexpress

OccurrenceCalculator Class

corelibraries-devexpress-dot-xtrascheduler-35313c3d.md

latest5.5 KB
Original Source

OccurrenceCalculator Class

Enables you to calculate the occurrences for a given recurrent appointment.

Namespace : DevExpress.XtraScheduler

Assembly : DevExpress.XtraScheduler.v25.2.Core.dll

NuGet Package : DevExpress.Scheduler.Core

Declaration

csharp
public abstract class OccurrenceCalculator
vb
Public MustInherit Class OccurrenceCalculator

The following members return OccurrenceCalculator objects:

Remarks

Use the OccurrenceCalculator object to calculate occurrences for a predefined time interval, as illustrated in the code sample below.

Note

The calculated appointments do not contain custom fields. This behavior is by design; it increases performance.

If you need to construct the actual recurrence appointment series with both standard and custom appointment fields, then you have to use the AppointmentPatternExpander.Expand method from the DevExpress.XtraScheduler.Native namespace. Create a new AppointmentPatternExpander instance using the Appointment.RecurrencePattern value of your appointment, and then call the Expand method to get the appointments collection.

Example

The following code sample retrieves the recurrence pattern of the first appointment in the scheduler’s collection and replaces all occurrences with appointments of AppointmentType.Normal type. It utilizes the OccurrenceCalculator.CalcOccurrences method to calculate items in the recurrent series.

Note

A complete sample project is available at https://github.com/DevExpress-Examples/winforms-scheduler-recurrence-series-occurrencecalculator

csharp
// Calculate occurrences for the first recurrent series.
Appointment pattern = schedulerStorage1.Appointments.Items.FirstOrDefault(item => item.Type == AppointmentType.Pattern);
if (pattern == null) return;
OccurrenceCalculator calc = OccurrenceCalculator.CreateInstance(pattern.RecurrenceInfo);
TimeInterval processedInterval = new TimeInterval(DateTime.Today, DateTime.Today.AddDays(7));
AppointmentBaseCollection calculatedOccurrences = calc.CalcOccurrences(processedInterval, pattern);

// Create normal appointments in place of occurrences.
schedulerStorage1.BeginUpdate();
for (int i = 0; i < calculatedOccurrences.Count; i++) {
    Appointment resultAppointment = schedulerStorage1.CreateAppointment(AppointmentType.Normal);
    resultAppointment.Subject = String.Format("Occurrence {0} - {1}", calculatedOccurrences[i].Subject, i);
    resultAppointment.Description = calculatedOccurrences[i].Description;
    resultAppointment.Start = calculatedOccurrences[i].Start;
    resultAppointment.End = calculatedOccurrences[i].End;
    schedulerStorage1.Appointments.Add(resultAppointment);
}
schedulerStorage1.EndUpdate();

// Remove the pattern and occurrences.
schedulerStorage1.Appointments.Remove(pattern);
vb
' Calculate occurrences for the first recurrent series.
Dim pattern As Appointment = schedulerStorage1.Appointments.Items.FirstOrDefault(Function(item) item.Type = AppointmentType.Pattern)
If pattern Is Nothing Then
    Return
End If
Dim calc As OccurrenceCalculator = OccurrenceCalculator.CreateInstance(pattern.RecurrenceInfo)
Dim processedInterval As New TimeInterval(Date.Today, Date.Today.AddDays(7))
Dim calculatedOccurrences As AppointmentBaseCollection = calc.CalcOccurrences(processedInterval, pattern)

' Create normal appointments in place of occurrences.
schedulerStorage1.BeginUpdate()
For i As Integer = 0 To calculatedOccurrences.Count - 1
    Dim resultAppointment As Appointment = schedulerStorage1.CreateAppointment(AppointmentType.Normal)
    resultAppointment.Subject = String.Format("Occurrence {0} - {1}", calculatedOccurrences(i).Subject, i)
    resultAppointment.Description = calculatedOccurrences(i).Description
    resultAppointment.Start = calculatedOccurrences(i).Start
    resultAppointment.End = calculatedOccurrences(i).End
    schedulerStorage1.Appointments.Add(resultAppointment)
Next i
schedulerStorage1.EndUpdate()

' Remove the pattern and occurrences.
schedulerStorage1.Appointments.Remove(pattern)

Inheritance

Object OccurrenceCalculator

See Also

OccurrenceCalculator Members

DevExpress.XtraScheduler Namespace