简体   繁体   English

如何使用设置在自定义类中保存多维数组

[英]How to save multidimensional array in custom class with settings

I have a class which i wish to save instances of within my settings file. 我有一个类,希望将其实例保存在我的设置文件中。 I am unable to save the class using the regular properties.settings.default.save() command. 我无法使用常规的properties.settings.default.save()命令保存该类。 No error will come up, but the settings will not persist between runs, which i understand to be a problem with serialization. 没有错误会出现,但是设置不会在两次运行之间保持不变,我知道这是序列化的问题。

I have pinpointed the problem to one specific line, a public multidimensional int array. 我已将问题精确定位到一个特定的行,即公共多维int数组。 If i change the 2d array to a 1d array, it works fine, but a 2d array is preferred for my application. 如果我将2d数组更改为1d数组,它可以正常工作,但是2d数组更适合我的应用程序。 As i am still learning, i would like to know specifically why this happens. 在我仍在学习的同时,我想特别了解为什么会这样。

My question is, why wont the 2d array save into the settings? 我的问题是,为什么二维数组不会保存到设置中? is it a problem of serialization? 这是序列化的问题吗? How can i implement this correctly? 我该如何正确实施?

public class AsmbLine 
{

    public int linenumber { get; set; }
    public Ticket[] ticketlist { get; set; }
    public string[] platesizes { get; set; }
    public string[] platetypes { get; set; }
    public string[] platecounts { get; set; }
    public int[,] cellidsallowed { get; set; } // This Line is the problem

    //public int[] cellidsallowed { get; set; } //uncomment these two lines to fix
    //public int[] cellidsallowedtypes { get; set; } // uncomment these two lines to fix

Cheers 干杯

** Edit ** **编辑**

Here is how i access / save my settings. 这是我访问/保存设置的方式。 i find when using custom classes that its easiest to use a seperate bool to flag if the settings is initialized for the first time start-up 我发现在使用自定义类时,如果设置是首次启动初始化的,最容易使用单独的bool进行标记

private void asmbinitializedcheckandload()
    {
            bool temp = false;
            temp = Properties.Settings.Default.Asmbarrayinitialized;
            if (temp == false) // if this is false, then we are running for the first time
            {
                Optimo20.Properties.Settings.Default.alines = new AsmbLine[maxlines]; //create new instance of alines[]

                Properties.Settings.Default.Save(); // save

                for (int i = 0; i < Properties.Settings.Default.alines.Length; i++)
                {
                    Optimo20.Properties.Settings.Default.alines[i] = new AsmbLine(); // initialize each member of alines


                }    
                Properties.Settings.Default.Asmbarrayinitialized = true; // set the flag to true, setting is initialized
                Properties.Settings.Default.Save(); // save
             }

        alinearray = Optimo20.Properties.Settings.Default.alines; // load settings data into local array.


    }

What happens when you change your code to this..? 当您将代码更改为此时,会发生什么? this should Compile 这应该编译

public class AsmbLine 
{
    public int linenumber { get; set; }
    public Ticket[] ticketlist { get; set; }
    public string[] platesizes { get; set; }
    public string[] platetypes { get; set; }
    public string[] platecounts { get; set; }
    //public int[] cellidsallowed { get; set; } //uncomment these two lines to fix
    private int[,] cellidsallowed;  

    public int[,] Cellidsallowed         
    {   get { return cellidsallowed; } 
        set { cellidsallowed = value; }         
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM