Ok so I will try to make this short.
I am implementing undo/redo functionality in an UML diagram editor. I have objects of classes, all implementing my VisualUMLComponent interface.
I have a class, VisualUMLCollection, implementing ObservableCollection<VisualUMLComponent> to keep track of all objects (classes, interfaces, arrows, comments, abstracts, etc) to draw on my Canvas object. This works fine.
Now to the point.
I try to implement undo/redo, with having each object having a pointer to a PreviousState and NextState of type VisualUMLComponent. As such, when I change an object, I can first Clone() it, and insert it at the proper place in the doubly linked list.
I have made class for the undo/redo functionality. It has two stacks undoStack and redoStack of type Stack<DiagramChange>. DiagramChange class represents changes in the diagram. It can have three types: Add, Remove and Alter, for when adding an object, removing an object, or changing an object respectively.
My problem is with the code below, that I get an ArgumentOutOfRangeException ÃÂ ÃÂ at System.Windows.Media.VisualCollection.Insert(Int32 index, Visual visual), when I try to .Add an old state object to my VisualUMLCollection: