3

I'm saving and loading various variables in my application settings file (Settings.settings). Saving/Loading variables such as strings, Uris and DataTables is working correctly.

When attempting to Save/Load a custom object List<IterationFilter>, the List is lost when the application is closed and reopened. List<IterationFilters> becomes null when the application is reloaded... even though an IterationFilter was added to the List and saved.

Saving a String (working correctly):

Properties.Settings.Default.ConnectionString = connectionString;
Properties.Settings.Default.Save();

Saving a Generic List:

Properties.Settings.Default.FilterList.Add(newFilter);
Properties.Settings.Default.Save();

I followed this answer, to create my List setting. My .settings file looks like this:

Visual Studio Screenshot showing a settings file

[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public System.Collections.Generic.List<TFS_Extraction.IterationFilter> FilterList {
    get{
            return ((System.Collections.Generic.List<TFS_Extraction.IterationFilter>)(this["FilterList"]));
       }
    set{
            this["FilterList"] = value;
       }
}

My IterationFilter class:

namespace TFS_Extraction
{
[Serializable]
public class IterationFilter
{
    public string Operator { get; set; }
    public string Value { get; set; }

    public IterationFilter(string _operator, string _value)
    {
        Operator = _operator;
        Value = _value;
    }
}    
Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75

1 Answers1

3

TFS_Extraction.IterationFilter has to be serializable. The class is required to have a public default constructor.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
Kostya
  • 849
  • 5
  • 14
  • 1
    I added `[Serializable]` directly before my IterationFilter class definition, but the setting is still not being persisted – Kyle Williamson Sep 29 '17 at 17:39
  • @Kyle Williamson try adding the default constructor public IterationFilter() {...} – Kostya Sep 29 '17 at 17:52
  • 1
    For XML serialization [Serializable] attribute is not necessary – Kostya Sep 29 '17 at 17:54
  • Adding a default constructor to the `IterationFilter` class fixed my issue! I tried removing `[Serializable]` and the List is still being saved. If you can update your answer to include a default constructor... I can mark this as the accepted answer. Thanks – Kyle Williamson Sep 29 '17 at 17:58
  • @Kyle Williamson No problem. – Kostya Sep 29 '17 at 18:03