0

I have a custom class and a collection thereof that I want to use to create entries in my Settings.default. I managed to set the types in the designer using the browse(...) button on my namespace, and in a given session, it works. I can store and get values from my classes (or rather, objects of those classes).

But the settings don't get saved. I have used this method to implement my custom classes in my Settings, and I'm trying tips such as this one to have them saved - but no luck here.

How will I be able to save custom object classes? The types within the final class are trivial, nothing but strings, integers and booleans.

Update: It's not a collection serialization issue, a simple class containing a single string also won't persist. I changed my code to a simpler example.

using System;
using System.Windows.Forms;
using System.Configuration;

namespace tester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if(Properties.Settings.Default.MySetting== null)
            {
                MessageBox.Show("MySetting is null");
                Properties.Settings.Default.MySetting = new saveme();  // this saveme will be accessible just fine for the duration of the session, but it won't persist.
            }
            textBox1.Text = Properties.Settings.Default.MySetting.somestring;  // will always be "initial value" as per initialization of saveme()
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.MySetting.somestring = textBox1.Text;
            Properties.Settings.Default.Save();
        }
    }

    [Serializable()]
    public class saveme : ApplicationSettingsBase  // class containing data
    {
        private string Somestring = "initial value";

        [UserScopedSetting()]
        [DefaultSettingValue("default value")]
        [SettingsSerializeAs(SettingsSerializeAs.Binary)]
        public string somestring
        {
            get { return Somestring; }
            set { Somestring = value; }
        }
    }
}

It will always end up with MySetting being null

Community
  • 1
  • 1
Ben Philipp
  • 1,832
  • 1
  • 14
  • 29

1 Answers1

2

I was able to reproduce the non-persisting issue and was able to make things work.

Here is a simple working example if you want to create a new setting by code.

public partial class Form1 : Form
{
    private FormSettings frmSettings1 = new FormSettings();

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = frmSettings1.FormText;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        frmSettings1.FormText = textBox1.Text;
        this.Close();
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        frmSettings1.Save();
    }

}

//Application settings wrapper class
sealed class FormSettings : ApplicationSettingsBase
{
    [UserScopedSettingAttribute()]
    public String FormText
    {
        get { return (String)this["FormText"]; }
        set { this["FormText"] = value; }
    }
}

I did not add a new setting on the project properties settings tab. I also avoided using the Properties.Settings.Default.

I found out that Properties.Settings.Default does not persist and by testing it only works (persist) for the settings added to the project properties settings tab.

In case you want to try it you should instead use MyNameSpace.Properties.Settings.Default instead of plain Properties.Settings.Default. It makes a difference.

jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • You are absolutely right! I would've never guessed I could simply construct a new object inheriting the ApplicationSettingsBase and save to it without using the "proper" settings :) Thank you so much!! – Ben Philipp May 03 '16 at 04:05
  • Thank you very much.A couple of days ago Visual Studio started complaining that Properties.Settings.Default did not contain the values I put in the project properties settings GUI tool. Your class works properly instead. – Jack Griffin Jan 05 '17 at 17:34