0

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?

JoshVarty
  • 9,066
  • 4
  • 52
  • 80
Adrao
  • 412
  • 5
  • 18
  • 1
    You're going to have to show all of your implementation. We can't guess as to what the problem is with only a couple lines of code. – Jonathon Reinhart Mar 09 '13 at 22:08
  • What undo / redo framework are you talking about? Is it this one : http://www.codeproject.com/Articles/19550/Automated-Undo-Redo-library-Csharp-Generics-NET-C ? – andri Mar 09 '13 at 22:25

4 Answers4

3

If you modify BG_Blend and store a reference to it in the stack, all your references in the stack will point at the same instance. You need to create a new instance when you store it in the stack. Make a copy of the ColorBlend before it is changed and store the copy in the stack.

gilly3
  • 87,962
  • 25
  • 144
  • 176
1

In the setter you assign

_BG_Blend = value;                   

in the getter you return

return _BG_Blend;

So, yes, you will get the last assigned value no matter what's in your stack.

It looks like in the getter you need to return the value from the stack, not the backing field value.

gdoron
  • 147,333
  • 58
  • 291
  • 367
  • that is done by the UndoRedoFramework everytime one of the stacks changes it updates the parrent object – Adrao Mar 09 '13 at 22:13
  • 2
    @user2147134, so maybe... you will show us all your code and how you're using it? our crystal ball is off-line since Monday. – gdoron Mar 09 '13 at 22:15
1

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.

cara
  • 1,012
  • 1
  • 9
  • 15
0

Your _BG_Blend member variable is static, so there is only ever one instance of it. Every time you push a new blend you overwrite the static variable with the new value, but as all your undo steps just point at this single shared value, they all get "overwritten" by it.

Just remove the static modifier.

Jason Williams
  • 56,972
  • 11
  • 108
  • 137