Search code examples
c#undo-redo

Undo Redo Stack


I'm making a simple UndoRedo Framework using Stacks but for some reason something is not working. I have this code to store an object's property, in this case a ColorBlend

public static ColorBlend _BG_Blend = new ColorBlend();
public ColorBlend BG_Blend
{
    get { return _BG_Blend; }
    set
    {
        AddLog("BG_Blend", _BG_Blend); //Name && Property Value
        _BG_Blend = value;                   
    }
}

Now every time I change the blend this stores the new property in the stack and sends a signal that a new action can be undone. If have lets say 5 different ColorBlends in the Log, when i hit Undo it returns the stored properties, but they all have the same colors, positions everything. Anyone knows why?


Solution

  • The problem is that you always store the same object reference in your stack (namely _BG_Blend), which means that each entry in your stack points to the same object, to be more precise the object you inserted last. For each blend you should store a new reference.