3

I have a class that stores settings for my app. It is instantiated when the app. runs and saved when the app. closes.

public class Settings
{
    public bool showPrivacyPageOnBlogs;
    public bool showTermsPageOnBlogs;
    public bool showDisclosurePageOnBlogs;
}

And there is a popup window that displays check boxes to set these values using public properties of the popup window.

The code to handle the popup window is like:

// Horrible code ahead
private void pagesSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    pagesSettingsForm.showPrivacyPageOnBlogs = settings.showPrivacyPageOnBlogs;
    pagesSettingsForm.showTermsPageOnBlogs = settings.showTermsPageOnBlogs;
    pagesSettingsForm.showDisclosurePageOnBlogs = settings.showDisclosurePageOnBlogs;
    if (pagesSettingsForm.ShowDialog() == DialogResult.OK)
    {
        settings.showPrivacyPageOnBlogs = pagesSettingsForm.showPrivacyPageOnBlogs;
        settings.showTermsPageOnBlogs = pagesSettingsForm.showTermsPageOnBlogs;
        settings.showDisclosurePageOnBlogs = pagesSettingsForm.showDisclosurePageOnBlogs;
    }
    pagesSettingsForm.Dispose();
}

In my app. there are several more parameters being handled in this way so I would like to know if there is some way to simplify this code to maybe enumerate the names of the settings and allow for future addition of additional parameters.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Andy
  • 157
  • 10

3 Answers3

2

Though I haven't tried that, but I strongly believe that Automapper can handle this. I think it may make your code to be like this:

// Horrible code ahead
private void pagesSettingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
    Mapper.Map(settings, pagesSettingsForm);
    if (pagesSettingsForm.ShowDialog() == DialogResult.OK)
        Mapper.Map(pagesSettingsForm, settings);
    pagesSettingsForm.Dispose();
}    

P.S.: I know that you said that code is horrible, but I can't not to mention that you're disposing a form which is instantiated in some other code - which is wrong, IMO.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Snowbear
  • 16,924
  • 3
  • 43
  • 67
  • Hi, I accidentally cut out the instantiation line of code which was part of the method when copy and pasting the code. – Andy Nov 28 '11 at 07:18
2

Simply have the form expose a property of type Settings with a getter and a setter. That makes the snippet you posted simple without any changes needed when you add members to Settings. The effort now moves to the form implementation. PropertyGrid is a generic object editor, whether it is usable enough in your case is hard to guess.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
  • Kind of like you suggested, I simply passed a reference to the settings object to the form. And the form code updates the settings when an ok button is clicked. Will check out the PropertyGrid. Thanks. – Andy Nov 28 '11 at 07:23
1

Use a dictionary

Dictionary<String,dynamic> 

would be nice as it would defer typing

So you add/set settings in your settings class for all the setting you need app wise. You form has it's own instance of it, but only with the ones it needs. Then it would be just matter of scanning down form.settings for same named versions in the passed settingsw and overwriting them.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39