填充 2 列多维数组 VB.net
我在向多维数组赋值时遇到问题。
我正在尝试构建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用列表而不是数组。
.Net 对数组类型和集合类型进行了区分。数组类型应该具有固定的大小,因此像您想要的那样附加到末尾效果不佳。集合意味着更加灵活。
无论如何,为什么您希望通过分配来附加到数组的末尾!?您最好的希望就是替换整个阵列。
Use a List instead of an array.
.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.