473,396 Members | 2,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Complex data types in User-scoped ApplicationSettings

How are complex data types saved and restored when used as User-scoped
Application Settings? For an example of what I'm trying to do that does not
work, take the following:

A project has a user-scoped Settings object called "TestStrings" which is of
type "StringDictionary". The following code is run:

if (Settings.Default.TestStrings == null)
Settings.Default.TestStrings = new StringDictionary();
Settings.Default.TestStrings.Add("Test", "Value");
Settings.Default.Save();

Stepping through that code reveals that at each line, the proper action is
performed successfully -- TestStrings is instantiated as a new dictionary,
and a new KeyValuePair is added to it. But each time this program is run,
TestString is *always* null. Other string variables stored in the same
Settings instance are loaded and saved correctly, but TestStrings is not.

Are there any special tricks to dealing with complex objects in
ApplicationSettings?

May 17 '06 #1
2 12799
Hi,

Thank you for posting!

The problem that StringDictionary type settings is not saved is because it
doesn't have a TypeConverter and not xml serializable.

There are two primary mechanisms that ApplicationSettingsBase uses to
serialize settings:
1) If a TypeConverter exists that can convert to and from string, we use it.
2) If not, we fallback to the XmlSerializer.

Here we can derive a custom type from StringDictionary and either implement
a TypeConverter for it or implement IXmlSerializable.

Following is a sample which implements IXmlSerializable:

public class SerializableStringDictionary : StringDictionary,
IXmlSerializable
{

#region Node class
[Serializable]
public class Node
{
public Node()
{ }

public Node(string k, string v)
{
key = k;
val = v;
}

public string key;
public string val;
}

#endregion Node class for XML Serialization

#region IXmlSerializable Members

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer x = new
XmlSerializer(typeof(System.Collections.ArrayList) , new System.Type[] {
typeof(Node) });

reader.Read();
ArrayList list = x.Deserialize(reader) as ArrayList;

if (list == null)
return;

foreach (Node node in list)
{
Add(node.key, node.val);
}
}

public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer x = new
XmlSerializer(typeof(System.Collections.ArrayList) , new System.Type[] {
typeof(Node) });
ArrayList list = new ArrayList();
foreach (string key in this.Keys)
{
list.Add(new Node(key, this[key]));
}
x.Serialize(writer, list);
}

#endregion
}
Hope this helps.

Regards,

Walter Wang
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.

May 18 '06 #2
I'am have the same problem.

Do us have the example in VB.NET or have other solution?

I need urgent this.

Best Regards.
______________________________________

"Walter Wang [MSFT]" wrote:
Hi,

Thank you for posting!

The problem that StringDictionary type settings is not saved is because it
doesn't have a TypeConverter and not xml serializable.

There are two primary mechanisms that ApplicationSettingsBase uses to
serialize settings:
1) If a TypeConverter exists that can convert to and from string, we use it.
2) If not, we fallback to the XmlSerializer.

Here we can derive a custom type from StringDictionary and either implement
a TypeConverter for it or implement IXmlSerializable.

Following is a sample which implements IXmlSerializable:

public class SerializableStringDictionary : StringDictionary,
IXmlSerializable
{

#region Node class
[Serializable]
public class Node
{
public Node()
{ }

public Node(string k, string v)
{
key = k;
val = v;
}

public string key;
public string val;
}

#endregion Node class for XML Serialization

#region IXmlSerializable Members

public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}

public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer x = new
XmlSerializer(typeof(System.Collections.ArrayList) , new System.Type[] {
typeof(Node) });

reader.Read();
ArrayList list = x.Deserialize(reader) as ArrayList;

if (list == null)
return;

foreach (Node node in list)
{
Add(node.key, node.val);
}
}

public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer x = new
XmlSerializer(typeof(System.Collections.ArrayList) , new System.Type[] {
typeof(Node) });
ArrayList list = new ArrayList();
foreach (string key in this.Keys)
{
list.Add(new Node(key, this[key]));
}
x.Serialize(writer, list);
}

#endregion
}
Hope this helps.

Regards,

Walter Wang
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.

Jul 17 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Chris Travers | last post by:
Hi all; I just made an interesting discovery. Not sure if it is a good thing or not, and using it certainly breakes first normal form.... Not even sure if it really works. However, as I am...
116
by: Mike MacSween | last post by:
S**t for brains strikes again! Why did I do that? When I met the clients and at some point they vaguely asked whether eventually would it be possible to have some people who could read the data...
2
by: Pete | last post by:
Before I get started with the question, does anyone have a (single) good book recommendation for database design? Not an Access-specific book, but something geared toward helping me figure out...
8
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. ...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
2
by: (PeteCresswell) | last post by:
Seems like the toolkit does not handle complex data types. But that seems like much of the real world - once you get past something like just looking up a SIC code or adding two numbers. I see...
0
by: greg | last post by:
We have a set of .NET web services to provide access to our data. They use complex data types for both incoming data and return data. We are attempting to help another group access this web...
2
by: Arvid Requate | last post by:
Hello, I'd like to understand why the following code does not compile. It looks like a strangeness in connection with overload resolution for the <complex> header: The conversion operator...
2
by: ma740988 | last post by:
I'm looking for an equivalent approach to what I would do in C++ such to create a vector of vector of complex. I would do : typedef std::vector < std::complex < double complex_dvec; typedef...
11
by: jacob navia | last post by:
hi I am trying to use the complex data type. Consider this code, taken from the cclib library: struct complex csqrt(Cpx z) { double r; r=sqrt(z.re*z.re+z.im*z.im);...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.