Question

I followed Trouble saving a collection of objects in Application Settings to save my ObservableCollection of custom objects which is bound to a DataGrid in the application settings, but the data is not stored in user.config like the other settings. Can somebody help me with this? Thank you!

My custom classes:

[Serializable()]
public class ActuatorParameter
{
    public ActuatorParameter()
    {}
    public string caption { get; set; }
    public int value { get; set; }
    public IntRange range { get; set; }
    public int defaultValue { get; set; }
}

and

[Serializable()]
public class IntRange
{
    public int Max { get; set; }
    public int Min { get; set; }

    public IntRange(int min, int max)
    {
        Max = max;
        Min = min;
    }
    public bool isInRange(int value)
    {
        if (value < Min || value > Max)
            return true;
        else
            return false;
    }
}

Fill collection and save:

Settings.Default.pi_parameters = new ObservableCollection<ActuatorParameter> 
{ 
new ActuatorParameter() { caption = "Velocity", range = new IntRange(1, 100000), defaultValue = 90000},
new ActuatorParameter() { caption = "Acceleration", range = new IntRange(1000, 1200000), defaultValue = 600000},
new ActuatorParameter() { caption = "P-Term", range = new IntRange(150, 350), defaultValue = 320},
new ActuatorParameter() { caption = "I-Term", range = new IntRange(0, 60), defaultValue = 30},
new ActuatorParameter() { caption = "D-Term", range = new IntRange(0, 1200), defaultValue = 500},
new ActuatorParameter() { caption = "I-Limit", range = new IntRange(0, 1000000), defaultValue = 2000}
};
Settings.Default.Save();

My custom setting:

internal sealed partial class Settings
{
    [global::System.Configuration.UserScopedSettingAttribute()]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    public ObservableCollection<ActuatorParameter> pi_parameters
    {
        get
        {
            return ((ObservableCollection<ActuatorParameter>)(this["pi_parameters"]));
        }
        set
        {
            this["pi_parameters"] = value;
        }
    }
}
Was it helpful?

Solution

After long research I finally found out that the IntRange class was missing a standard constructor.

[Serializable()]

public class IntRange
{
    public int Max { get; set; }
    public int Min { get; set; }

    public IntRange()
    {}

    public IntRange(int min, int max)
    {
        Max = max;
        Min = min;
    }
   public bool isInRange(int value)
   {
        if (value < Min || value > Max)
            return true;
        else
            return false;
   }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top