Pergunta

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?

Foi útil?

Solução 2

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.

Outras dicas

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.

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.

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.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top