473,394 Members | 1,739 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,394 software developers and data experts.

Storing Custom Classes in User Settings?

Hi,

I'm not having a lot of luck googling for this one, I want to be able to
store a custom class in the user settings (DotNet2.0, win app). I don't
wish to create public get / set properities for all the things I want to
persist just for storing in the user settings file (because that would allow
other apps to access / change data they shouldn't be able to).

I understand how to browse to the object in VS2005 to add an entry to the
settings file., but I'm not sure what I need to do to my class to get it to
work. Using a very basic example below, is there an interface I should
inherit from? I'm guessing there must be an interface that defines at least
two methods, one that returns an xml string of the parameters to persist and
another that takes that xml string and allows the class to initalise itself
internally. (Note I really don't want to add Get/Set for val1 and val2 just
to get default persistance of all public properties).

Thanks in Advance,

- Paul

public ClassA
{
private int val1;
private string val2;

ClassA( val1, val2)
{
this.val1 = val1;
this.val2 = val2;
}

public bool Valid()
{
return ((val1 10) && !string.IsNullOrEmpty(val2));
}

public string Text()
{
return (val1.ToString() + ' ' + val2);
}
}
Jan 22 '07 #1
2 8315
The class must be serializable as XML. In order for it to be serializable as
XML, keep in mind that XML Serialization does not serialize classes without
parameterless constructors, so you need to add one to your class. In
addition, private fields, methods, and read-only (get only) properties are
not serialized. See http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx
for more information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.

"Paul Hadfield" <no****@noone.comwrote in message
news:eB**************@TK2MSFTNGP03.phx.gbl...
Hi,

I'm not having a lot of luck googling for this one, I want to be able to
store a custom class in the user settings (DotNet2.0, win app). I don't
wish to create public get / set properities for all the things I want to
persist just for storing in the user settings file (because that would
allow other apps to access / change data they shouldn't be able to).

I understand how to browse to the object in VS2005 to add an entry to the
settings file., but I'm not sure what I need to do to my class to get it
to work. Using a very basic example below, is there an interface I should
inherit from? I'm guessing there must be an interface that defines at
least two methods, one that returns an xml string of the parameters to
persist and another that takes that xml string and allows the class to
initalise itself internally. (Note I really don't want to add Get/Set for
val1 and val2 just to get default persistance of all public properties).

Thanks in Advance,

- Paul

public ClassA
{
private int val1;
private string val2;

ClassA( val1, val2)
{
this.val1 = val1;
this.val2 = val2;
}

public bool Valid()
{
return ((val1 10) && !string.IsNullOrEmpty(val2));
}

public string Text()
{
return (val1.ToString() + ' ' + val2);
}
}


Jan 22 '07 #2
Thanks for your answer - when I couldn't find anything online I had wondered
if this was using the Xml Serialisation object as I've seen it before
posting objects on a message queue. I had hoped that you would be able to
either override the Xml Serialisation object for saving particular classes -
or have the object inherit from a common interface and provide specialised
functionality that the Xml Serialisation provides.

As I don't want to provide a default constructor and expose the private
attributes / change the read only properties; I guess the best solution
would be to create a thin helper class, that just contains the data I wish
to be saved (and no functionality) and then pass this into / from my class
before serialisation. That way I can keep the encapsulation in my class /
application and also use the functionality provided by Xml Serialisation.

- Paul.

"Kevin Spencer" <un**********@nothinks.comwrote in message
news:Od**************@TK2MSFTNGP06.phx.gbl...
The class must be serializable as XML. In order for it to be serializable
as XML, keep in mind that XML Serialization does not serialize classes
without parameterless constructors, so you need to add one to your class.
In addition, private fields, methods, and read-only (get only) properties
are not serialized. See
http://msdn2.microsoft.com/en-us/library/182eeyhh.aspx for more
information.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

In case of Minimalism, break Philip Glass.

"Paul Hadfield" <no****@noone.comwrote in message
news:eB**************@TK2MSFTNGP03.phx.gbl...
>Hi,

I'm not having a lot of luck googling for this one, I want to be able to
store a custom class in the user settings (DotNet2.0, win app). I don't
wish to create public get / set properities for all the things I want to
persist just for storing in the user settings file (because that would
allow other apps to access / change data they shouldn't be able to).

I understand how to browse to the object in VS2005 to add an entry to the
settings file., but I'm not sure what I need to do to my class to get it
to work. Using a very basic example below, is there an interface I
should inherit from? I'm guessing there must be an interface that
defines at least two methods, one that returns an xml string of the
parameters to persist and another that takes that xml string and allows
the class to initalise itself internally. (Note I really don't want to
add Get/Set for val1 and val2 just to get default persistance of all
public properties).

Thanks in Advance,

- Paul

public ClassA
{
private int val1;
private string val2;

ClassA( val1, val2)
{
this.val1 = val1;
this.val2 = val2;
}

public bool Valid()
{
return ((val1 10) && !string.IsNullOrEmpty(val2));
}

public string Text()
{
return (val1.ToString() + ' ' + val2);
}
}



Jan 22 '07 #3

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

Similar topics

13
by: Ron L | last post by:
I have an application we are building that will be used by a number of different users on shared PCs. I want to be able to store a number of user settings in a location that is unique for each...
7
by: Martin Schulze | last post by:
Hello, i tried to compose myself a custom usercontrol which is derieved from System.Windows.Forms.UserControl. It contains 2 comboboxes and one textbox (which are also custom controls, but...
5
by: Graham | last post by:
I have created a custom MembershipProvider called "LassieMembershipProvider" that derives from "MembershipProvider". This providor is located in a Businesslogic layer dll called...
2
by: John | last post by:
Hi I was working fine with create user wizard and the default membership provider. I have now customised the membership provider as per attached web.config. The create user wizard picks up the...
0
by: Michael | last post by:
Hi, I would like to know if it is possible to use the ConfigurationSection class to modify user settings. I have created custom configuration classes that derive from ConfigurationSetion and...
3
by: Mark | last post by:
I'm consuming a webservice that makes a simple object available. The object class is marked in the web service as . I have a web application that consumes and uses this web service's class. When...
3
by: =?Utf-8?B?Um9iIEw=?= | last post by:
I am trying to use a custom type inside settings.settings. When I create my class, I am able to add it in as the type and the value ends up showing as XML data. If I change any of the values in...
0
by: Claire | last post by:
In project settings.default you can store "application" readonly settings and "user" read/write settings. Is there anyway to write application read/write settings rather than using the registry...
11
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
I have worked with application settings in VS2005 and C# for awhile, but usually with standard types. I have been trying to store a custom container/class/type in an application setting and I have...
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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.