Kyle Williamson
Kyle Williamson

Reputation: 2184

Why is this generic list not being saved in application settings?

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;
    }
}    

Upvotes: 3

Views: 228

Answers (1)

Kostya
Kostya

Reputation: 849

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

Upvotes: 3

Related Questions