Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
73 views
in Technique[技术] by (71.8m points)

c# - Loading custom Struct through settings.settings won't load content of list

I have my custom struct:

[Serializable]
public struct Point {
     public uint xPoint { get; }
     public uint yPoint { get; }

     public Point(uint x, uint y) {
          xPoint = x;
          yPoint = y;
     }
}

Then in my settings.settings.xml I have my var:

<Setting Name="navCoordList" Type="System.Collections.Generic.List&lt;MyNamespace.Form1.Point&gt;" Scope="User">
  <Value Profile="(Default)">

In my actual program I save a list of coordinates to my custom struct like:

pathList.Add(new Point(xValue, yValue));//3-50 of these in pathList

Then I'm saving this list to my settings.settings var like this:

Properties.Settings.Default.navCoordList= new List<Point>();
for (int i = 0; i < pathList.Count; i++) {
     Properties.Settings.Default.navCoordList.Add(pathList[i]);
     System.Diagnostics.Debug.WriteLine("Saved: " + Properties.Settings.Default.navCoordList[i].xPoint);
     }

     Properties.Settings.Default.Save();

So far everything is worked like I want them to. If I check my navCoordList[0].xPoint I get 425 (the correct coordinate).

Then when I exit the program and re-open it I'm trying to load my settings like this:

if (Properties.Settings.Default.navCoordList!= null) {
     if (Properties.Settings.Default.navCoordList.Count > 0) {
          for (int i = 0; i < Properties.Settings.Default.navCoordList.Count; i++) {
               pathList.Add(Properties.Settings.Default.navCoordList[i]);
                    }
                }
            }

My problem is here. It successfully saves the amount of items in my Point, but it will not save the contents of the list. So if I try to run System.Diagnostics.Debug.WriteLine(pathList[0].xPoint); it just returns 0. Same if I try to check Properties.Settings.Default.navCoordList[0]. The result is always 0.

It does know that I have 5 items in my list, which is the correct amount I saved in the previous session.

I have tried deleting my user.config file many times to work from scratch, but that has been no help.

question from:https://stackoverflow.com/questions/65853862/loading-custom-struct-through-settings-settings-wont-load-content-of-list

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...