I implemented an Undo/Redo system in my application but because it is a separate class from my main form it cannot access any of its controls. Currently I’m passing every control that needs undoing or redoing in to the class’s constructor but it is quickly becoming one of the largest constructors in my project.
It’d be nice if only this class were allowed to access to my form’s controls. If I pass in the form to the constructor I still cannot access the control’s because they are all private. I suppose the simple solution would be to make all the controls I need to use public but what is the ideal or elegant solution?
P.S. I have already read about the technique of having a public property for everything required and passing in the form to the constructor but that will end up being quite a bit of properties and the solution does not seem very applicable in this situation.
Thanks!
Very, very, very rarely is it good practice to pass UI controls or forms to a class. This is just a maintenance/dependency nightmare in the making.
You don’t want your class to know diddly about your form’s controls, all it should care about is its data.
Your form knows itself, knows the data it needs to save and how to restore it to the proper controls.
If all you are looking for is undo/redo capability, you want to look into the Memento design pattern. You don’t need to make a fully propertied class unless you have some other business need for it.
For the memento pattern.
A simple object holds the state you want to preserve….
A caretaker object manages the mementos for the form.
The form saves a new memento to the Caretaker after changing state. We take advantage of the extreme flexibility of the object class…
The form reverts as needed.
Forgot IMemento –just a tagging interface, in case you want to put the caretaker in another assembly for full Memento pattern implementation.