ResetEvents Issue

Feb 29, 2012 at 1:09 PM
Edited Feb 29, 2012 at 1:10 PM
I've noticed a little weird behavior in the operation of ResetEvents when used in WPF.

I have a simple app using loop to create a list of TimelineEvent objects. I add these to the timeline by calling ResetEvents(list). I noticed that no events were getting plotted on the control. So i stepped throught he code following the call to ResetEvents(List<TimelineEvent>)


public void ResetEvents(
            List<TimelineEvent>                         events

            // fix event dates
            foreach (TimelineEvent e in events)
                if (!e.IsDuration)
                    e.EndDate = e.StartDate;



When the method is called, the "events" parameter is fully populated with the list I created in the loop. As soon as m_events.Clear() is called the list "events" is zeroized. So when the foreach loop starts there is nothing to iterate and the loop ends immediately.

Any ideas why a m_events.Clear() call would cause the parameter to the ResetEvents method to be cleared also?

So in debugging I set up the app to use a simple XML (Monet.xml) data source (i.e. not the dynamically created List<TimelineEvent>)  and it renders the events just fine.

Feb 29, 2012 at 4:37 PM

Good catch! It is because both reference to the same collection. Workaround for you to pass this parameter line ResetEvents(e.ToList()), which would create a copy. 

Feb 29, 2012 at 5:40 PM

The .toList() fix works just fine.. Thanks for the help Andrew