我試圖添加一個項目到數組2d。將項目添加到陣列2D
但是,這將引發異常與此類似的消息: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"
我做錯了嗎?
而且......我可以在同一時間這樣設定的兩個維度?:
ReDim Preserve MyArray(MyArray.GetUpperBound(0)+1, MyArray.GetUpperBound(1)+1)
MyArray(lastitem) = {"Item 3,0", "Item 3,1"}
UPDATE:
嗯......否則嘗試它,我不噸得到它,代碼不起作用:
' 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"
感謝您提供的信息,但問題沒有解決,我仍然不知道如何解決問題,請參閱我的更新,如果可以的話。 – ElektroStudios
@ElektroStudios,你說得很對;我添加了一個可能的解決方案。 –