473,397 Members | 2,033 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,397 software developers and data experts.

Storing object array in Application.Settings

Hi All

How can I store an array of objects in my C# Application.Settings?

I have CMyObject class for which an array is created:

CMyObject[] arr = new CMyObject[2];
arr[0] = new CMyObject();
arr[0].Name = "One";

arr[1] = new CMyObject();
arr[1].Name = "Two";

Properties.Settings.Default.MyObjList = arr; // MyObjList is defined as
CMyObject[] and has "user" scope.
Properties.Settings.Default.Save();

The above code does not work and complains that CMyObject class is not
marked Serializable. So I have tried marking the class serializable as
follows and tried. That too does not work and complains about the same
thing.

[Serializable]
public class CMyObject
{
.....
}
Any suggestions?

TIA
Nayan
Jun 6 '06 #1
3 18255
Nayan,

How complext is your object heiarchy? Meaning, does CMyObject reference
other objects which might not be serializable?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nayan Mansinha" <nm*******@icode.com> wrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
Hi All

How can I store an array of objects in my C# Application.Settings?

I have CMyObject class for which an array is created:

CMyObject[] arr = new CMyObject[2];
arr[0] = new CMyObject();
arr[0].Name = "One";

arr[1] = new CMyObject();
arr[1].Name = "Two";

Properties.Settings.Default.MyObjList = arr; // MyObjList is defined as
CMyObject[] and has "user" scope.
Properties.Settings.Default.Save();

The above code does not work and complains that CMyObject class is not
marked Serializable. So I have tried marking the class serializable as
follows and tried. That too does not work and complains about the same
thing.

[Serializable]
public class CMyObject
{
....
}
Any suggestions?

TIA
Nayan

Jun 6 '06 #2
Hi Nicholas

Thank you for your reply.

CMyObject is the main class. It is not derived from any other class. Here
is the definition:

[Serializable]
public class CMyObject
{
private string FName;
private string FAddress;

public string Name
{
get { return FName; }
set { FName = value; }
}

public string Address
{
get { return FAddress; }
set { FAddress = value; }
}
}

Thanks.

Nayan

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:%2****************@TK2MSFTNGP05.phx.gbl...
Nayan,

How complext is your object heiarchy? Meaning, does CMyObject
reference other objects which might not be serializable?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nayan Mansinha" <nm*******@icode.com> wrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
Hi All

How can I store an array of objects in my C# Application.Settings?

I have CMyObject class for which an array is created:

CMyObject[] arr = new CMyObject[2];
arr[0] = new CMyObject();
arr[0].Name = "One";

arr[1] = new CMyObject();
arr[1].Name = "Two";

Properties.Settings.Default.MyObjList = arr; // MyObjList is defined as
CMyObject[] and has "user" scope.
Properties.Settings.Default.Save();

The above code does not work and complains that CMyObject class is not
marked Serializable. So I have tried marking the class serializable as
follows and tried. That too does not work and complains about the same
thing.

[Serializable]
public class CMyObject
{
....
}
Any suggestions?

TIA
Nayan


Jun 6 '06 #3
Okay, first, your class must be serializable as XML. This does not require
marking it as serializable, but making it serializable. For example, a class
with all serializable members is already serializable. Example:

public class Foo
{
private string _FooName = "";
public string FooName
{
get { return _FooName; } set { _FooName = value; }
}

public int FooNumber = 0;

public Foo() {}
}

Because the class contains all serialzable members, and it has a
parameterless constructor, and the public property has both a get and a set
method, and the types of all members of the class are serializable, the
class can be serialized as XML.

If you have a Collection or array of these, ensure that the Collection is
serializable. Some are, some aren't. The Array class is serializable.

Next, make sure that there is an element in the Settings file of the type
that you want to store (it can be empty, but it must exist). Depending upon
what sort of application you're creating, this can be done by hand-coding,
or via the Project Properties dialog.

That should take care of it.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Development Numbskull

Nyuck nyuck nyuck
"Nayan Mansinha" <nm*******@icode.com> wrote in message
news:uH**************@TK2MSFTNGP03.phx.gbl...
Hi All

How can I store an array of objects in my C# Application.Settings?

I have CMyObject class for which an array is created:

CMyObject[] arr = new CMyObject[2];
arr[0] = new CMyObject();
arr[0].Name = "One";

arr[1] = new CMyObject();
arr[1].Name = "Two";

Properties.Settings.Default.MyObjList = arr; // MyObjList is defined as
CMyObject[] and has "user" scope.
Properties.Settings.Default.Save();

The above code does not work and complains that CMyObject class is not
marked Serializable. So I have tried marking the class serializable as
follows and tried. That too does not work and complains about the same
thing.

[Serializable]
public class CMyObject
{
....
}
Any suggestions?

TIA
Nayan

Jun 6 '06 #4

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

Similar topics

8
by: Google Mike | last post by:
I need an Application object replacement. I was creating my own using shared memory -- shm* API in PHP. I was doing fine for 6 months until it just got too fat, I guess, and the RH9 Linux server...
0
by: alock | last post by:
I am looking for different opinions on how the state data of a .NET application should be stored. An example would be the size and location of the main application window. This allows the users'...
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...
6
by: (PeteCresswell) | last post by:
User wants to go this route instead of storing pointers in the DB and the documents outside. Only time I tried it was with only MS Word docs - and that was a loooong time ago - and it seemed to...
2
by: Paul Hadfield | last post by:
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...
6
by: J055 | last post by:
Hi I have the following code. I upload an XML file using the FileUpload object, store the stream in a session so the user gets the chance to confirm some options then pass the stream from the...
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...
11
by: =?Utf-8?B?cGJjb2Rlcg==?= | last post by:
Hi, I have been asked to create a solution in a single exe - using C# .NET 3.5. I have found that I can us ILMerge to create a single exe of all of my Application assemblies. But my...
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?
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
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
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.