【问题标题】:how to make 1-index based array如何制作基于 1 索引的数组
【发布时间】:2013-10-14 01:17:33
【问题描述】:

这是一个简单的问题 - 如何在 VB.NET 中创建一个索引从 1 开始的对象数组?

我需要这样一个对象来将范围写回到 Excel 电子表格中,并且它只接受基于 1 的索引。

当我从 Excel 读取范围时,它会在 VB.NET 中自动创建一个基于 1 的对象,但是当我尝试创建另一个对象时,它不允许我将 lBound 设置为 1。

【问题讨论】:

  • 为什么不为基于 0 的索引填充一个空值?
  • 因为 xlObject.Range("myrange") = VBNETObj 需要一个从 1 开始的对象。实际上,我可以复制我之前从 Excel 中读取的一个对象,它将是基于 1 的索引,但不能创建一个新的。
  • 我明白了....我会编写一个继承自 System.Collections.CollectionBase 的自定义类。 msdn.microsoft.com/en-us/library/xth2y6ft(v=vs.71).aspx
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。
  • @J-MANMORGAN:请不要发布 .NET 1.1 链接,除非问题是关于 .NET 1.1。那篇文章中的所有链接也是 .NET 1.1 链接,因此读者可能会陷入 .NET 1.1 文章的迷宫。

标签: arrays vb.net excel


【解决方案1】:

你可以使用Array.CreateInstance来实现你想要的。

    ' create an array of 10 items with lower bound index of 1
    Dim arrayStartingWith1 As Array = Array.CreateInstance(GetType(Integer), New Integer(0) {10}, New Integer(0) {1})

    ' this is now incorrect
    ' arrayStartingWith1(0) = 1

    ' this is correct
    arrayStartingWith1(1) = 1
    arrayStartingWith1(10) = 1

【讨论】:

  • 复制粘贴时,赋值语句出现错误。
  • s/b .SetValue 和 .GetValue
  • @dbasnett 哪一行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2015-07-19
  • 1970-01-01
相关资源
最近更新 更多