Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent DDay.ical ics files from repeating duplication events during SerializeToString function call

Hope some one can help. Using the DDay.iCal version 1.0.1.490 with .net version 2 I get duplicate events events after calling the SerilizeToString method.

Example Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DDay.iCal;
using DDay.iCal.Serialization.iCalendar;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            iCalendar iCal = new iCalendar();
            Event evt = iCal.Create<Event>();
            Uri eventLink = new Uri("http://middlebury.edu";);
            evt.IsAllDay = false;

           evt.Start = new iCalDateTime(DateTime.Parse("2011-08-11"));
           evt.Duration = new TimeSpan(2, 0, 0);
           evt.Location = "Test";
           evt.Summary = "Breakfast";
           evt.Url = eventLink;
           evt.Description = "Sausage Links" + "\n" + "Pancakes" + "\n";

          iCal.Events.Add(evt);

          iCalendarSerializer serializer = new iCalendarSerializer(iCal);

          string result = serializer.SerializeToString(iCal);
      }
   }
}
like image 242
Travis Stafford Avatar asked Aug 18 '11 16:08

Travis Stafford


1 Answers

The line Event evt = iCal.Create<Event>() creates a new event, adds it to the calendar's event collection, and returns it. Later, you manually add the same event to the calendar's event collection: iCal.Events.Add(evt)

I was doing the same thing, not realizing that the Create() method adds the event to the calendar. Either use a standard constructor to initialize the event, Event evt = new Event(), or remove the manual Add() to the calendar's event collection.

like image 73
DanielMason Avatar answered Nov 13 '22 17:11

DanielMason