在 vb.net 中构建多维数组 [英] Building a multidimensional array in vb.net

查看:21
本文介绍了在 vb.net 中构建多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个多维数组,该数组将为数据库中的每个记录保存两位信息,例如ID,描述.

这就是我目前正在做的事情.

Dim mArray(,) As StringDim i As Integer = 0而 cmdReader.Read()mArray(i,0) = cmdReader.Item("id")mArray(i,1) = cmdReader.Item("描述")我 = 我 + 1结束时间

我在这里遇到的问题是它不喜欢 mArray(i,0) 中的 i.有人对此有任何想法吗?这是给定的错误 未将对象引用设置为对象的实例.

感谢您的任何帮助.

纳鲁姆

解决方案

为什么不使用 列表类字典类

您可以创建一个字典列表,键和值都是字符串.然后,键可以代表您的键(示例中的 ID 和描述,而值可以是存储的内容).

类似的东西

Dim values As New List(Of Dictionary(Of String, String))()

然后在while循环中类似

values.Add(New Dictionary(Of String, String)() From { _{"id", cmdReader.Item("id")} _})values.Add(New Dictionary(Of String, String)() From { _{描述",cmdReader.Item(描述")} _})

然后你可以使用 foreach

For Each value As Dictionary(Of String, String) In valuesDim id As String = value("id")Dim description As String = value("description")下一个

或者一个 for

For i As Integer = 0 To values.Count - 1Dim value As Dictionary(Of String, String) = values(i)Dim id As String = value("id")Dim description As String = value("description")下一个

I'm trying to build up a multidimensional array which will hold two bits of info for each record in a database e.g. id, description.

This is what I am currently doing.

Dim mArray(,) As String
Dim i As Integer = 0
While cmdReader.Read()
    mArray(i,0) = cmdReader.Item("id")
    mArray(i,1) = cmdReader.Item("description")
    i = i + 1
End While

The problem I have here is that it doesn't like the i in mArray(i,0). Anyone have any ideas about this? This is the error that is given Object reference not set to an instance of an object.

Thanks for any and all help.

Nalum

解决方案

Why not rather make use of List Class and Dictionary Class

You can rather then create a List of Dictionaries, with the key and value both strings. The key can then represent your key (id and description in your example, and the value can be what ever was stored).

Something like

Dim values As New List(Of Dictionary(Of String, String))()

and then in the while loop something like

values.Add(New Dictionary(Of String, String)() From { _
    {"id", cmdReader.Item("id")} _
})
values.Add(New Dictionary(Of String, String)() From { _
    {"description", cmdReader.Item("description")} _
})

You could then use foreach

For Each value As Dictionary(Of String, String) In values
    Dim id As String = value("id")
    Dim description As String = value("description")
Next

Or a for

For i As Integer = 0 To values.Count - 1
    Dim value As Dictionary(Of String, String) = values(i)
    Dim id As String = value("id")
    Dim description As String = value("description")
Next

这篇关于在 vb.net 中构建多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆