PurpSchurp
PurpSchurp

Reputation: 646

2D arrays throws index out of bound exception

I'm making a program and I keep getting the error Index was outside the bounds of the array and I can't figure out why. I tried all the different versions of setting up my 2D array. Here is how I have it set up now

Dim dataArray(,) As Array = New Array(,) {{}}

Here is my loop for it

    Dim x As Integer
    Dim DT As DataTable
    Dim TA As New DSOldOrdersTableAdapters.TA
    DT = getOldOrders()
    For x = 0 To DT.Rows.Count - 1
        dataArray(0, x) = DT.Rows(x).Item("SO")
        dataArray(1, x) = (DT.Rows(x).Item("Customer"))
        dataArray(2, x) = (DT.Rows(x).Item("ShipBy"))
    Next

Upvotes: 1

Views: 726

Answers (3)

pmcoltrane
pmcoltrane

Reputation: 3112

Dim dataArray(,) As Array = New Array(,) {{}}

This creates a two-dimensional array of Array, i.e. a two-dimensional array where each element is an Array. It's then initialized to an array containing a single, empty Array element. I expect this is not what you intended.

Since you never ReDim your array anywhere in the code to alter its dimensions, it remains a two-dimensional array whose first dimension is length 1, and whose second dimension is length zero.

When you try to run

dataArray(0, x) = DT.Rows(x).Item("SO")

The second dimension of the array has length zero, so it cannot hold a value. Thus you get an "index out of range" exception. If this did not happen, you would probably get another exception, because DT.Rows(x).Item("SO") probably is not an Array.

It would probably be easier to leave the data in the DataTable, and read from it whenever needed. Otherwise, I think your intent was to do something like:

Dim dataArray(0 To 3, -1) As Object  'Temporarily create a 2D Object array

'...load the DataTable

ReDim dataArray(0 to 3, 0 To DT.Rows.Count - 1)  'Redimension the array to the proper size.

'...rest of code

Upvotes: 1

Steven Doggart
Steven Doggart

Reputation: 43743

You are declaring an array with a length of 0. That means that it will be unable to hold any data. All indexes will be out of range. Arrays do not automatically grow as items are added to them. As such, arrays should typically only be used in situations where the size of the array is fixed (unchanging). For instance:

Dim dataArray(2, 2) As Object  ' Creates a 3x3 array of objects

If you want it to automatically grow as items are added, you would typically want to use a List(Of T) rather than an array. For instance:

Public Class MyItem
    Public Property SO As Object
    Public Property Customer As Object
    Public Property ShipBy As Object
End Class

' ...

Dim dataList As New List(Of MyItem)()

' ...

Dim item As New MyItem()
item.SO = DT.Rows(x).Item("SO")
item.Customer = DT.Rows(x).Item("Customer")
item.ShipBy = DT.Rows(x).Item("ShipBy")
dataList.Add(item)

' ...

Label1.Text = dataList(1).SO

Or, if you insist on using an array to store each item, you can make a list of 1D arrays, like this:

Dim dataList As New List(Of Object())()

' ...

dataList.Add(
    {
    DT.Rows(x).Item("SO"),
    DT.Rows(x).Item("Customer"),
    DT.Rows(x).Item("ShipBy")
    })

' ...

Label1.Text = dataList(1)(0)

Upvotes: 3

keenthinker
keenthinker

Reputation: 7830

As @Steven Doggart said (and beat me up to one minute with the answer) you are declaring an array, but you don't give the dimension length. You have two options:

  • specify the array dimension size at the declaration
  • or use Redim to set the (dimension) size of the array

In your case one solution could look like this:

Dim dataArray(,) As Array = New Array(3, DT.Rows.Count) {{}}

Or like this:

Dim dataArray(,) As Array = New Array(,) {{}}
Redim dataArray(3, DT.Rows.Count)
Dim x As Integer
Dim DT As DataTable
Dim TA As New DSOldOrdersTableAdapters.TA
DT = getOldOrders()
For x = 0 To DT.Rows.Count - 1
    dataArray(0, x) = DT.Rows(x).Item("SO")
    dataArray(1, x) = (DT.Rows(x).Item("Customer"))
    dataArray(2, x) = (DT.Rows(x).Item("ShipBy"))
Next

Have a look at this MSDN Article - How to: Initialize an Array variable in VB.NET.

Upvotes: 1

Related Questions