Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building a multidimensional array in vb.net

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

like image 637
Nalum Avatar asked Nov 23 '10 10:11

Nalum


People also ask

How do you declare a multidimensional array in Visual Basic?

In visual basic, Multidimensional Arrays can be declared by specifying the data type of an elements followed by the brackets () with comma (,) separator. Following are the examples of creating two or three-dimensional arrays in visual basic programming language.

How do you create a multidimensional array?

Creating Multidimensional Arrays You can create a multidimensional array by creating a 2-D matrix first, and then extending it. For example, first define a 3-by-3 matrix as the first page in a 3-D array. Now add a second page. To do this, assign another 3-by-3 matrix to the index value 2 in the third dimension.

What is multidimensional array and example?

A multi-dimensional array is an array with more than one level or dimension. For example, a 2D array, or two-dimensional array, is an array of arrays, meaning it is a matrix of rows and columns (think of a table). A 3D array adds another dimension, turning it into an array of arrays of arrays.

What is multidimensional array and its syntax?

Array-Basics in Java Multidimensional Arrays can be defined in simple words as array of arrays. Data in multidimensional arrays are stored in tabular form (in row major order). Syntax: data_type[1st dimension][2nd dimension][]..


1 Answers

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
like image 76
Adriaan Stander Avatar answered Oct 27 '22 02:10

Adriaan Stander