Storing object array in Application.Settings

  • Thread starter Thread starter Nayan Mansinha
  • Start date Start date
N

Nayan Mansinha

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
 
Nayan,

How complext is your object heiarchy? Meaning, does CMyObject reference
other objects which might not be serializable?
 
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 said:
Nayan,

How complext is your object heiarchy? Meaning, does CMyObject
reference other objects which might not be serializable?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Nayan Mansinha said:
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
 
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
 
Back
Top