tags:

views:

51

answers:

3

How do winforms options systems work?

I've seen one article which implements the functionality via xml serialisation. Is it possible to implement this functionality via a class using static or constant variables? I can't quite remember, but do consts/static variables maintain state even when the application is closed?

Of course, I know the key is in maintaining state even when the app is closed and for that, even a database can be used (overkill).

Thanks

+3  A: 

Check out Application Settings in .Net 2.0 or higher http://msdn.microsoft.com/en-us/library/aa730869(VS.80).aspx

Gerrie Schenck
Hi, that is just what I am after. However, I can't assign selected options to the Settings. E.g: Settings.Default.Background = opt.colorDialog1.Color.Name; throws a read only error for Background. Background is my setting in the Settings. Options is an ptions form and colorDialog1 is a color dialog in the Options form.
dotnetdev
Are you sure you've set the Scope to User?
Gerrie Schenck
Thanks for that, fixed :) Does that mean each user on the machine or do I need a login system (I assume the former). Why can't I write values when using Application.
dotnetdev
All settings are saved in the application's config file I think. The difference between system and user is simply that your program can access the user ones, and the system ones are supposed to be fixed on deployment. Of course you can always edit the config file to change these. Check out MSDN, it will be out there somewhere.
Gerrie Schenck
A: 

Variables do not maintain state when the application is closed. You will need to store the values you want to maintain somewhere and read them back when needed.

Fredrik Mörk
A: 

Because the state maintained by the process is destroyed when a process terminates it is neccessary to persist any data which must suurvive termination to non-volitile storage such as the hard disk and serialisation is one way to achieve this.

Serialization is popular because it creates an abstraction which allows the application to save and restore data without neccessarily having to be concerned with the specifics of how the data is to be stored or retrieved.

While ultimatly the information must end up in non-volitile storage there are other methods of persisting the information which have different benefits and drawbacks.

The main attraction of serialization is that one needs not be concerned with the specifics of the storage as this is determined by reflection on the members of the object being persisted, however this reflection can also be seen as a disadvantage with respect to the often large performance hit incurred by reflecting over a type, further to this it is quite common for objects to be persisted to XML, the parsing of which can be quite intensive in terms of resource usage.

The serialization API supports different formatters such as the binary formatter which typically generates much smaller files, although I don't think the output of this formatter is garenteed to be consistant accross windows versions.

One other storage option is the registry API which is often faster then generating or parsing XML, however it's use is somewhat discouraged for new applcations

I hope this has been somewhat helpful, I can't help but feel I may have rambled just a little

Crippledsmurf