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