How to save custom collections using ApplicationSettingsBase?

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.
 
G

Gary Chang[MSFT]

Hi Mountain,
What am I missing? This code only saves an empty
element like this:

<setting name="TestCollection" serializeAs="Xml">
<value />
</setting>

It appears the corresponding SettingsProvider cannot serialize your custom
type property to the disk file.

When the LocalFileSettingsProvider serializes the property to disk, it
first attempts to call the ConvertToString or ConvertFromString on the
type's associated TypeConverter. If this does not succeed, it uses XML
serialization instead.

I suggest you need to implement a TypeConverter to your custom type class,
please refer to the following MSDN2 documentation for the further
information:

How to: Implement a Type Converter
http://msdn2.microsoft.com/en-us/library/ayybcxe5(VS.80).aspx

Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

I was hoping that deriving from ApplicationSettingsBase would simplify the
implementation of application configuration settings. If I am forced to
implement TypeConverters for the types I define, then using
ApplicationSettingsBase becomes just as much work as not using
ApplicationSettingsBase.

Is there not a way to make the XML serialization suffice for a config file?

How can I proceed without going thru the work to implement a TypeConverter?
If a TypeConverter is my only choice, I think the old .NET 1.1 way of doing
things is better than deriving from ApplicationSettingsBase. Am I right?

Thanks,
Mountain

P.S. Is there any documentation that mentions the need for a TypeConverter?
I did not see this in my searches related to ApplicationSettingsBase. Can you
provide me with a link? Thanks.

"Gary Chang[MSFT]" said:
Hi Mountain,
What am I missing? This code only saves an empty
element like this:

<setting name="TestCollection" serializeAs="Xml">
<value />
</setting>

It appears the corresponding SettingsProvider cannot serialize your custom
type property to the disk file.

When the LocalFileSettingsProvider serializes the property to disk, it
first attempts to call the ConvertToString or ConvertFromString on the
type's associated TypeConverter. If this does not succeed, it uses XML
serialization instead.

I suggest you need to implement a TypeConverter to your custom type class,
please refer to the following MSDN2 documentation for the further
information:

How to: Implement a Type Converter
http://msdn2.microsoft.com/en-us/library/ayybcxe5(VS.80).aspx

Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gary Chang[MSFT]

Hi,
Is there not a way to make the XML serialization suffice
for a config file?

How can I proceed without going thru the work to
implement a TypeConverter?

It appears your custom type is somewhat complicated for XML serialization.
I suggest you refer to the example in the following MSDN document, you may
need to define your custom type as that:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlserializationxmlserializerclasstopic.asp

P.S. Is there any documentation that mentions the need for a
TypeConverter?

I suggest you can refer to the paragraph "Settings Serialization" in the
following MSDN2 documentation, it introduces the serialization actions when
using the LocalFileSettingsProvider:

Application Settings Architecture
http://msdn2.microsoft.com/en-us/library/8eyb2ct1(VS.80).aspx


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Gary,
Thanks for your suggestions. So far, however, none of this has helped.
David

I thought the whole idea of the
"Gary Chang[MSFT]" said:
Hi,
Is there not a way to make the XML serialization suffice
for a config file?

How can I proceed without going thru the work to
implement a TypeConverter?

It appears your custom type is somewhat complicated for XML serialization.
I suggest you refer to the example in the following MSDN document, you may
need to define your custom type as that:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemxmlserializationxmlserializerclasstopic.asp

P.S. Is there any documentation that mentions the need for a
TypeConverter?

I suggest you can refer to the paragraph "Settings Serialization" in the
following MSDN2 documentation, it introduces the serialization actions when
using the LocalFileSettingsProvider:

Application Settings Architecture
http://msdn2.microsoft.com/en-us/library/8eyb2ct1(VS.80).aspx


Thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006. Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Gary Chang[MSFT]

Hi David,

I am regret to know that sample doesn't help. It appears your custom type
is somewhat complicated to XML serialization.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top