470,631 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,631 developers. It's quick & easy.

How to save custom collections using ApplicationSettingsBase?

I'm working with the example code found at
http://msdn2.microsoft.com/en-us/lib...se(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.
Apr 6 '06 #1
5 16229
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/lib...e5(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.

Apr 7 '06 #2
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]"" wrote:
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/lib...e5(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.

Apr 7 '06 #3
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/de...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/lib...t1(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.

Apr 10 '06 #4
Gary,
Thanks for your suggestions. So far, however, none of this has helped.
David

I thought the whole idea of the
""Gary Chang[MSFT]"" wrote:
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/de...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/lib...t1(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.

Apr 14 '06 #5
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.

Apr 17 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by =?Utf-8?B?Qy4gSHVnaGVz?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.