填充 2 列多维数组 VB.net

发布于 2024-12-12 10:07:25 字数 617 浏览 0 评论 0原文

我在向多维数组赋值时遇到问题。

我正在尝试构建一个 2 列、无限行数组并填充来自数据读取器的数据。

Dim tblArry(,) As String = {}

If reader.HasRows Then
  While reader.Read()
      ' Call Read before accessing data.
      Dim tempTbl As String = "tblLang" & reader.Item(0)
      Dim tempTblTwoLett As String = reader.Item(1)

      tblArry = New String(tempTbl, tempTblTwoLett)

' Tried this too....
tblArry = {New String(tempTbl, New String(tempTblTwoLett)} 
' and this...
tblArry = New String(tempTbl), New String(tempTblTwoLett)

当我使用锯齿状数组时,我可以让它工作。 Reader 部分工作得很好,但编码对于这个问题来说并不干净。

任何建议将不胜感激。

谢谢

I am having issues assinging values to a multi-dimensional array.

I am trying to build a 2 col, unlimited row array and populate with data from a data reader.

Dim tblArry(,) As String = {}

If reader.HasRows Then
  While reader.Read()
      ' Call Read before accessing data.
      Dim tempTbl As String = "tblLang" & reader.Item(0)
      Dim tempTblTwoLett As String = reader.Item(1)

      tblArry = New String(tempTbl, tempTblTwoLett)

' Tried this too....
tblArry = {New String(tempTbl, New String(tempTblTwoLett)} 
' and this...
tblArry = New String(tempTbl), New String(tempTblTwoLett)

When I use a Jagged Array I can get this to work. The Reader portion is working just fine, but the coding is just not clean for this problem.

Any suggestions would be appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-12-19 10:07:25

使用列表而不是数组。

Dim langs As New List(Of String())()

While reader.Read()
  Dim temp(1) As String
  temp(0) = "tblLang" & reader.Item(0)
  temp(1) = reader.Item(1)

  langs.Add(temp)
End While

.Net 对数组类型和集合类型进行了区分。数组类型应该具有固定的大小,因此像您想要的那样附加到末尾效果不佳。集合意味着更加灵活。

无论如何,为什么您希望通过分配来附加到数组的末尾!?您最好的希望就是替换整个阵列。

Use a List instead of an array.

Dim langs As New List(Of String())()

While reader.Read()
  Dim temp(1) As String
  temp(0) = "tblLang" & reader.Item(0)
  temp(1) = reader.Item(1)

  langs.Add(temp)
End While

.Net makes a distinction between array types and collections types. Array types are meant to have fixed sizes, so appending to the end like you want to do doesn't work well. Collections are meant to be more flexible.

Regardless, why in the world would you expect to append to the end of an array by assigning to it!? The best you could hope for there is that you replace the entire array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文