Reputation: 190
I've set up a console app to test out Ical.net. I have this code that is pulling the occurrences of these events from the calendar (which is working great); but I want to be able to view the other details that exist within these event objects; and I don't see in the documentation how to view the details.
Here is my code:
var calendar = new Calendar();
var recurrenceRule = new RecurrencePattern("FREQ=DAILY");
var vEvent = new CalendarEvent
{
Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
Description = "Test Description on event object",
Name = "Event Title 1",
RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);
recurrenceRule = new RecurrencePattern("FREQ=DAILY;INTERVAL=4;WKST=SU");
vEvent = new CalendarEvent
{
Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
Description = "Test2 event description yo!",
Name = "Event Title 2",
RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);
var searchStart = DateTime.Parse("2018-10-01");
var searchEnd = DateTime.Parse("2018-10-31");
var occurrences = calendar.GetOccurrences(searchStart, searchEnd);
foreach (var item in occurrences)
{
Console.WriteLine(item.ToString());
}
Console.ReadLine();
Updated
I was able to view some of the event details by adding the following before my Console.ReadLine();
Code Addition:
foreach (var item in calendar.Children)
{
Console.WriteLine(item.Name);
}
My only issue now is how would I tie up the occurrence date, this item.name, and the event description together into one object / things that reference each other?
Complete Code:
var calendar = new Calendar();
var recurrenceRule = new RecurrencePattern("FREQ=DAILY");
var vEvent = new CalendarEvent
{
Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
Description = "Test Description on event object",
Name = "Event Title 1",
RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);
recurrenceRule = new RecurrencePattern("FREQ=DAILY;INTERVAL=4;WKST=SU");
vEvent = new CalendarEvent
{
Start = new CalDateTime(DateTime.Parse("2018-10-01T07:00")),
End = new CalDateTime(DateTime.Parse("2018-10-01T08:00")),
Description = "Test2 event description yo!",
Name = "Event Title 2",
RecurrenceRules = new List<RecurrencePattern> { recurrenceRule }
};
calendar.Events.Add(vEvent);
var searchStart = DateTime.Parse("2018-10-01");
var searchEnd = DateTime.Parse("2018-10-31");
var occurrences = calendar.GetOccurrences(searchStart, searchEnd);
foreach (var item in occurrences)
{
Console.WriteLine(item.ToString());
}
foreach (var item in calendar.Children)
{
Console.WriteLine(item.Name);
//result: Event Title 1, Event Title 2
//Does not have access to even description, start, or end values.
}
Console.ReadLine();
Upvotes: 1
Views: 2310
Reputation: 73313
Children
returns a list of ICalendarObject
.
If you want to retrieve the event details, you have to cast back to CalendarEvent
:
foreach (ICalendarObject item in calendar.Children)
{
Console.WriteLine(item.Name);
if (item is CalendarEvent calendarEvent)
{
Console.WriteLine(calendarEvent.Description);
Console.WriteLine(calendarEvent.Start);
Console.WriteLine(calendarEvent.End);
}
}
Output:
Event Title 2
Test2 event description yo!
01/10/2018 07:00:00
01/10/2018 08:00:00
To get the source event from the occurrences, you can do this:
foreach (var item in occurrences)
{
var sourceEvent = item.Source as CalendarEvent;
Console.WriteLine(sourceEvent.Description);
Console.WriteLine(sourceEvent.Start);
Console.WriteLine(sourceEvent.End);
}
Upvotes: 3