G
Guest
I'm working with the example code found at
http://msdn2.microsoft.com/en-us/library/system.configuration.applicationsettingsbase(VS.80).aspx.
I would like to implement application settings based on some custom types.
For example, let's say I have a collection of settings that I could represent
similar to this code:
[Serializable()]
public class MyItem
{
private int myValue;
public int MyValue
{
get { return myValue; }
set { myValue = value; }
}
public MyItem(int value)
{
myValue = value;
}
}
[Serializable()]
public class MyCollection
{
private List<MyItem> list = new List<MyItem>();
public void Add(MyItem newItem)
{
list.Add(newItem);
}
public List<MyItem> TheList
{
get { return list; }
set { list = value; }
}
}
Then, in my ApplicationSettingsBase-derived class, I could add this:
sealed class FormSettings : ApplicationSettingsBase
{
. . .
[UserScopedSetting()]
public MyCollection TestCollection
{
get { return (MyCollection)this["TestCollection"]; }
set { this["TestCollection"] = value; }
}
}
to load the settings, I could do something like this:
private void Form1_Load_1(object sender,
EventArgs e)
{
testCollection = frmSettings1.TestCollection;
if (testCollection == null)
{
testCollection = new MyCollection();
testCollection.Add(new MyItem(count++));
testCollection.Add(new MyItem(count++));
}
and to save the settings, I would do this:
private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
//Synchronize manual associations first.
frmSettings1.FormText = this.Text + '.';
testCollection.Add(new MyItem(count++));
frmSettings1.TestCollection = testCollection;
frmSettings1.Save();
}
What am I missing? This code only saves an empty element like this:
<setting name="TestCollection" serializeAs="Xml">
<value />
</setting>
Thanks.
http://msdn2.microsoft.com/en-us/library/system.configuration.applicationsettingsbase(VS.80).aspx.
I would like to implement application settings based on some custom types.
For example, let's say I have a collection of settings that I could represent
similar to this code:
[Serializable()]
public class MyItem
{
private int myValue;
public int MyValue
{
get { return myValue; }
set { myValue = value; }
}
public MyItem(int value)
{
myValue = value;
}
}
[Serializable()]
public class MyCollection
{
private List<MyItem> list = new List<MyItem>();
public void Add(MyItem newItem)
{
list.Add(newItem);
}
public List<MyItem> TheList
{
get { return list; }
set { list = value; }
}
}
Then, in my ApplicationSettingsBase-derived class, I could add this:
sealed class FormSettings : ApplicationSettingsBase
{
. . .
[UserScopedSetting()]
public MyCollection TestCollection
{
get { return (MyCollection)this["TestCollection"]; }
set { this["TestCollection"] = value; }
}
}
to load the settings, I could do something like this:
private void Form1_Load_1(object sender,
EventArgs e)
{
testCollection = frmSettings1.TestCollection;
if (testCollection == null)
{
testCollection = new MyCollection();
testCollection.Add(new MyItem(count++));
testCollection.Add(new MyItem(count++));
}
and to save the settings, I would do this:
private void Form1_FormClosing_1(object sender, FormClosingEventArgs e)
{
//Synchronize manual associations first.
frmSettings1.FormText = this.Text + '.';
testCollection.Add(new MyItem(count++));
frmSettings1.TestCollection = testCollection;
frmSettings1.Save();
}
What am I missing? This code only saves an empty element like this:
<setting name="TestCollection" serializeAs="Xml">
<value />
</setting>
Thanks.