0

I'm trying to add an Item to an Array 2d.

But this throws an exception with message like this: Redim only can change the dimension which is more to the right

' Create Array 2D

Dim MyArray As String(,) = _
{{"Item 0,0", "Item 0,1"}, {"Item 1,0", "Item 1,1"}, {"Item 2,0", "Item 2,1"}}


' Add Item

ReDim Preserve MyArray(MyArray.GetUpperBound(0)+1, MyArray.GetUpperBound(1)+1)
MyArray(MyArray.GetUpperBound(0), MyArray.GetUpperBound(1) - 1) = "Item 3,0"
MyArray(MyArray.GetUpperBound(0), MyArray.GetUpperBound(1)) = "Item 3,1"

What I'm doing wrong?

Also... I could set the two dimensions at the same time like this?:

ReDim Preserve MyArray(MyArray.GetUpperBound(0)+1, MyArray.GetUpperBound(1)+1)
MyArray(lastitem) = {"Item 3,0", "Item 3,1"}

UPDATE:

Well... trying it otherwise, I don't get it, the code does not work:

' Add Item

Dim MyArray2(MyArray.GetUpperBound(0) + 1, MyArray.GetUpperBound(1) + 1)(,) As String
MyArray.CopyTo(MyArray2, 0)
MyArray2(MyArray2.GetUpperBound(0), MyArray2.GetUpperBound(1) - 1) = "Item 3,0"
MyArray2(MyArray2.GetUpperBound(0), MyArray2.GetUpperBound(1)) = "Item 3,1"

2 Answers 2

2

As noted in the ReDim documentation:-

Resizing with Preserve. If you use Preserve, you can resize only the last dimension of the array, and for every other dimension you must specify the same bound it already has in the existing array.

For example, if your array has only one dimension, you can resize that dimension and still preserve all the contents of the array, because you are changing the last and only dimension. However, if your array has two or more dimensions, you can change the size of only the last dimension if you use Preserve.

So to resize the array in more than one dimension, you would need something like this:-

Dim second_array(my_array.GetUpperBound(0) + 1, my_array.GetUpperBound(1) + 1) As String

For i As Integer = 0 To my_array.GetUpperBound(0)
    For j As Integer = 0 To my_array.GetUpperBound(1)
        second_array(i,j) = my_array(i,j)
    Next j
Next i

my_array = second_array
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the information but the problem is not solved, I still have no idea of how to fix it, please see my update if you could.
@ElektroStudios, you are quite right; I've added a possible solution.
2

When you determine you need a larger array than you already have, you will need to define a new array rather than ReDim the existing one if the first dimension changes, then copy across all the contents.

Array(2,2) -> Array(2,4) 'ReDim will work for this.
Array(2,2) -> Array(4,2) 'Create a new array and copy the contents

1 Comment

thnaks but ...and how I do that?... sorry I need more help than a Create a new array and copy contents because I don't know how to fix it, I'm trying to write a generic function to add an item to a 2D array then I need to automatize arraybound calculations and those things. please see my update if you could.

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.