Combine Generics and Functional Programming: C# : Creating an Undo manager: Listing 1

You need to store a list of objects, which will facilitate enabling multiple undo and redo actions. Note that you previously declared the UndoRedoActions class as a private class. It's actually nested inside an Undo Manager.

public class UndoHandler

{
    private class UndoRedoActions { /* elided */ }

    private Stack<UndoRedoActions> undoStack 
        = new Stack<UndoRedoActions>();

    private Stack<UndoRedoActions> redoStack = 
        new Stack<UndoRedoActions>();

    public void ExecuteCommand(
        Action command, Action undoCommand)
    
	{
        command();
        undoStack.Push(new UndoRedoActions(
            command, undoCommand));
    }

    public void Undo()
    
	{
        if (undoStack.Count > 0)
        {
            UndoRedoActions cmd = undoStack.Pop();
            cmd.UndoAction();
            redoStack.Push(cmd);
        }
    }

    public void Redo()
    
	{
        if (redoStack.Count > 0)
        {
            UndoRedoActions cmd = redoStack.Pop();
            cmd.RedoAction();
            undoStack.Push(cmd);
        }
    }

    public bool CanUndo
    
	{
        get { return undoStack.Count > 0; }
    }

    public bool CanRedo
    
	{
        get { return redoStack.Count > 0; }
    }
}
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.