473,805 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.IsNullO rEmpty(val2));
}

public string Text()
{
return (val1.ToString( ) + ' ' + val2);
}
}
Jan 22 '07 #1
2 8345
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.c omwrote in message
news:eB******** ******@TK2MSFTN GP03.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.IsNullO rEmpty(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**********@n othinks.comwrot e in message
news:Od******** ******@TK2MSFTN GP06.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.c omwrote in message
news:eB******** ******@TK2MSFTN GP03.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.IsNullO rEmpty(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
1917
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 user, so that one user's settings do not affect another. Is there a "standard" location to store these files? How about a "standard" location to store a file of default settings if the user hasn't run the application yet? TIA Ron L
7
12506
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 directly derived from the .net classes ComboBox and TextBox) I did the custom control in the visual studio .net designer in which i
5
2559
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 "Enlighten.LinkMad.Businesslogic". In one of my frontend websites I use this type to authenticate a user who is trying to login. The following excerpt is from the web.config of the particular site showing the reference to the custom provider, allowing .Net to do...
2
3567
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 custom membership provider fine and removes the security question/answer fields as designated in the custom provider. The problem is that when I try to create a new user in the create user wizard by entering the info and pressing the 'create...
0
1093
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 which I access using the ConfigurationManager.GetSection method. This all works fine, but it only outputs to the app.config file. Is it possible to output the settings the user.config file?
3
2266
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 I receive the object from the web service, I'm interested in storing that object in ViewState in the web application, but I receive the error below. I'm not shocked that the web application can't serialize the object as the attribute isn't...
3
4399
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 my class, the values do not update the settings file. Can anyone provide any insight and/or sample code on how to use a user defined class as a valid and usable type in the settings.settings file? Thanks, --Rob.
0
1033
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 HKEY_LOCAL_MACHINE? My database connectionstring elements need storing but i dont want every user to need to complete the values. Or is there some other recommended method where i dont have to write my own files or use the registry. Also, how do...
11
10155
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 seen erratic results. I am aware of one known defect where user classes do not show up in the list of types on the Property/Settings page in the visual designer and I am wondering if I am encountering some other peculiar issue, or if there are...
0
9718
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10614
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10369
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3847
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.