2

For some reason, I can't seem to store an array of my class into the settings. Here's the code:

            var newLink = new Link();
            Properties.Settings.Default.Links = new ArrayList();
            Properties.Settings.Default.Links.Add(newLink);
            Properties.Settings.Default.Save();

In my Settings.Designer.cs I specified the field to be an array list:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public global::System.Collections.ArrayList Links {
        get {
            return ((global::System.Collections.ArrayList)(this["Links"]));
        }
        set {
            this["Links"] = value;
        }
    }

For some reason, it won't save any of the data even though the Link class is serializable and I've tested it.

ATL_DEV
  • 9,256
  • 11
  • 60
  • 102
  • 1
    "I can't seem" How do you see that? Is an exception thrown? Is the list empty when loading? Is is empty without reloading? – Foxfire May 03 '10 at 23:52
  • The list is empty with no exceptions thrown. Somehow it isn't serializing my Links. – ATL_DEV May 03 '10 at 23:54
  • Is the data contained in the settings file (it's an XML file so you can easily check)? – Foxfire May 03 '10 at 23:56
  • I checked the user.config and there are no Links contained in the array. – ATL_DEV May 03 '10 at 23:59
  • Here's the user.config file: Interestingly, I tried to write an array of integers and it worked fine. I wonder what gives? – ATL_DEV May 04 '10 at 00:05

2 Answers2

3

I found the source of the problem. Simply using a plain Array won't cut it. After thinking about it, the deserializer wouldn't know what type to deserialize the array items to. I failed to see that the array required strong typing. The designer lead me to foolishly believe it was a plain generic array:

    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public List<Link> Links
    {
        get {
            return ((List<Link>)(this["Links"]));
        }
        set {
            this["Links"] = value;
        }
    }

I had to make these changes in the Settings.Designer.cs and not from the designer.

ATL_DEV
  • 9,256
  • 11
  • 60
  • 102
1

Make sure that your Link class is either correctly XML-serializable or that it has a typeconverter to string (which is preferred when using application.settings files).

I'd assume that something in your types will not transform into the XML-serialization format. And your user.config shows that it doesn't have any string typeconverter available.

Foxfire
  • 5,675
  • 21
  • 29