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