10

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

5 Answers 5

14

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
Sign up to request clarification or add additional context in comments.

5 Comments

Would I then be able to loop over that using for/foreach? Don't know much about .net was thrown into it for this project.
Thanks astander, will give this a shot, looks like it will do the trick.
Took a bit of fiddling but I got it working, Thanks astander.
Any idea how to sort this? stackoverflow.com/questions/15819207/…
For me it says The given key was not present in the dictionary..
6

Try this

Dim mArray(1,1) 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
    ReDim Preserve mArray(i,1)
End While

Comments

3

The problem is that you are not initializing the array.

This should work, until i will not reach the limits set in the initialization.

Dim mArray(100,100) 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

But if the array limits are not known I suggest to follow astander's suggestion.

1 Comment

Unfortunately the limits are unknown, I tired doing Dim mArray(,2) but was told to put a number in for the first part of the array.
1

This works for me:

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

values.Add(New Dictionary(Of String, String)() From {{"quarter", q1.ToString}, {"year", y1.ToString}})
values.Add(New Dictionary(Of String, String)() From {{"quarter", q2.ToString}, {"year", y2.ToString}})
values.Add(New Dictionary(Of String, String)() From {{"quarter", q3.ToString}, {"year", y3.ToString}})
values.Add(New Dictionary(Of String, String)() From {{"quarter", q4.ToString}, {"year", y4.ToString}})

For Each value As Dictionary(Of String, String) In values
    Dim quarter As String = value("quarter")
    Dim year As String = value("year")
    Debug.Print(quarter & "/" & year)
Next

Comments

-2

Correct it by

Dim mArray(,) As String = ""

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.