【问题标题】:destination array was not long enough. check destindex and length and the array's lower bounds目标数组不够长。检查 destindex 和长度以及数组的下限
【发布时间】:2016-04-08 21:57:37
【问题描述】:

我有一堂课,代码如下:

  Array tags;
  if (lines.Length > 0)
  {
      configText = lines[0];
      tags = new Array[lines.Length];
      lines.CopyTo(tags,1);
  }

这里出现以下错误:

目标数组不够长。检查 destindex 和长度和 数组的下界。

方法:

     private bool ReadPointListFile(string fileName) {

        // Read each line of the file into a string array. Each element
        // of the array is one line of the file.
        string[] lines = System.IO.File.ReadAllLines(fileName);
        string configText = string.Empty;

        if (lines.Length > 0)
        {
            configText = lines[0];
            tags = new Array[lines.Length];
            lines.CopyTo(tags,1);
        }
        else
            lines.CopyTo(tags,0);

        GetConfigurationInfo(lines[0], out this.sInterval, out this.dataAggregate);

        return true;
    }

【问题讨论】:

  • 向我们展示整个代码。比如,“线条”的类型,它的内容等等

标签: c# arrays exception


【解决方案1】:

tags 是 Array 对象的数组,这可能不是您想要的。 如果要从字符串数组(行)复制,目标数组也应该是字符串数组。 所以,无论你在哪里声明标签,它都应该是string[] tags; 在你的 if 块中它应该是 tags = new string[lines.Length];

这是关于类型和 ArrayTypeMismatch Exception 的部分。

现在,如果您打算复制除第一个元素以外的所有元素,则不能使用CopyTo(tags, 1),因为 1 用于目标数组。它指示从哪里开始写入值。这就是为什么你有你的例外。 相反,只需执行循环: for(int i = 1; i tags = new string[lines.Length-1];

如果你想跳过行和标签数组中的第一个索引,那么它是tags = new string[lines.Length];tags[i-1] = lines[i];

【讨论】:

    【解决方案2】:

    它将从 1 个索引而不是从零索引开始复制,这会产生问题。 试试

    lines.CopyTo(tags,0);
    

    【讨论】:

    • 对于提到的代码:ArrayTypeMismatch Exception Occurs
    • 行的类型是什么。是数组还是列表?
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 2019-09-12
    • 1970-01-01
    • 2020-03-16
    • 2020-01-07
    • 2013-08-29
    • 2012-05-08
    • 2012-08-02
    相关资源
    最近更新 更多