【问题标题】:How to save multidimensional array in custom class with settings如何使用设置在自定义类中保存多维数组
【发布时间】:2014-08-19 19:06:21
【问题描述】:

我有一个类,我希望在我的设置文件中保存它的实例。我无法使用常规 properties.settings.default.save() 命令保存类。不会出现错误,但设置不会在运行之间持续存在,我理解这是序列化的问题。

我已将问题定位到一个特定的行,即公共多维 int 数组。如果我将二维数组更改为一维数组,它可以正常工作,但二维数组更适合我的应用程序。由于我仍在学习,我想具体了解为什么会发生这种情况。

我的问题是,为什么二维数组不能保存到设置中?是序列化的问题吗?我怎样才能正确地实现它?

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

干杯

** 编辑 **

这是我访问/保存设置的方式。我发现在使用自定义类时,最容易使用单独的 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.


    }

【问题讨论】:

  • 既然你正在学习这个网站将非常适合你如何设置你需要的东西dontetperls 2D array
  • 谢谢。 Dotnetperls 已经为我提供了巨大的帮助!

标签: c# class serialization save settings


【解决方案1】:

当您将代码更改为 this.. 时会发生什么?这应该编译

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

【讨论】:

  • 我花了一段时间才评论/取消评论所有内容以将其切换回来。不幸的是,它仍然没有正确保存到设置的相同错误。
  • 基于您发布的内容以及我尝试为您简化的内容..您需要显示更多代码..save settings 程序在哪里...?
  • 当然可以。在上面添加了更新。我基本上只是使用 properties.settings.default.save() 函数。其他设置被保存但那个没有,这肯定是因为那个 int[,] 的事情。
  • 您正在查看什么文件以查看更改是否持续存在您可以发布文件位置吗?我想知道您是否正在查看错误的文件
  • 我实际上不按说看文件。当我重新运行程序并尝试加载该值时,它只会为我保存的类加载 null,而 bool 标志加载“true”,因为我第一次将其设置为该值(默认值为 false)。我在这条线上检查它:alinearray = Optimo20....
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-09-24
  • 2010-12-01
  • 2021-07-02
  • 1970-01-01
  • 2018-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多