Building a multidimensional array in vb.net

前端 未结 5 1900
离开以前
离开以前 2020-12-10 05:30

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 doi

5条回答
  •  心在旅途
    2020-12-10 05:58

    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
    

提交回复
热议问题