tags:

views:

88

answers:

2

When adding a control to my form, currently I have to wire it up with my save and load code, with my internal data structures and I have to do this with all my controls. This scenario severely violates the DRY (don't repeat yourself) principle and can introduce subtle bugs.

I have came up with the idea of traversing all the Controls in a foreach loop, the Name property will be the key and the Text (or whatever depending on the type) will be the value in a dictionary (filtering for user input controls during the procedure). This way I will have to serialize/deserialize the dictionary to save/load it.

So, why am I asking? I am a beginner and I think there are more proven and tested methods for accomplishing the same task then what I came up with.

And sorry for my clunky English, I have not had the fortune to learn it as my first language.

Thanks for your help

note: I know about WPF, but I have to stick to .net 2.0

A: 

Ideally you want all the controls to inherit from a base class, the base class can then deal with all of this when each control is initialised. If you need the dictionary then pass the dictionary into a method, the method can then set all the various properties required on the control.

If each control inherits, then the logic is shared and DRY :)

Ben Hall
Sure, but if he's using WinForms, they already inherit from specific base classes.
Kyralessa
The base class could inherit from those classesFor example Control > MyControlwould becomeControl > MySetupBase > MyControl
Ben Hall
A: 

There are already good examples for doing that, see RealPosition. We modified this source to do form/control position saving in our project by just placing a component on the form in the designer and specifying the necessary properties there. Look at the IExtenderProvider and ISupportInitialize interfaces on MSDN too.

Filip Navara