# How to create a Multidimensional Array?

**URL:** <https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225>\
**Category:** Getting Started\
**Created:** [August 21, 2015, 11:05pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225 "2015-08-21T23:05:12Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [August 21, 2015, 11:05pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/1 "2015-08-21T23:05:12Z")

</div>

Sorry for my newbiness.

I want to make a multidimensional array for build a table like a recordset.  
But I don’t know how to do it.

I know thats the way to create a 2d Array:  
Dim my2Darray( -1,-1 ) As String

For example I need to create an array with 4 columns and 20 rows. (Unit, Quantity,Description,Price)

I want to learn how to do this.  
Thanks.

And then I need to know how can I get the value of each cell of the Array

Thanks

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 21, 2015, 11:17pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/2 "2015-08-21T23:17:13Z")

</div>

Dim my2Darray( 3, 19 ) As String // not the dimensions are the Ubounds

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 21, 2015, 11:24pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/3 "2015-08-21T23:24:00Z")

</div>

Or use Redim() to set the size dynamically.

Dim my2Darray(-1,-1) as string // create the array  
…  
rows = x  
columns = y  
redim my2Darray(row, columns) // size the array

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [August 22, 2015, 12:59am UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/4 "2015-08-22T00:59:10Z")

</div>

[quote=208817:@Tim Hare]Or use Redim() to set the size dynamically.

Dim my2Darray(-1,-1) as string // create the array  
…  
rows = x  
columns = y  
redim my2Darray(row, columns) // size the array[/quote]  
So, then if I want to add values. Is this the right way to do it?:  
for example for the headers:

my2Darray(0,0) = “unit”  
my2Darray(0,1) = “quantity”  
my2Darray(0,2) = “Description”  
my2Darray(0,3) = “Price”

Pls tell me if I’m wrong. 🙂

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 22, 2015, 1:04am UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/5 "2015-08-22T01:04:09Z")

</div>

thats right

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [August 24, 2015, 2:51pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/6 "2015-08-24T14:51:44Z")

</div>

And If I want to evaluate If the Array is empty or (,)?

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [August 24, 2015, 4:03pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/7 "2015-08-24T16:03:56Z")

</div>

Use Ubound ().

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [August 24, 2015, 6:03pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/8 "2015-08-24T18:03:31Z")

</div>

> [@209246:@Tim Hare](#):
>
> Gerardo García

Bound Only works for one dimensional Arrays ☹

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [August 24, 2015, 6:05pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/9 "2015-08-24T18:05:50Z")

</div>

It works for other dimensions  
[http://documentation.xojo.com/index.php/Ubound](http://documentation.xojo.com/index.php/Ubound)

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [August 24, 2015, 6:14pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/10 "2015-08-24T18:14:12Z")

</div>

`Dim arr(2, 3, 4) As String // array with 3 dimensions
// Note that Ubound is one-based:
Dim ub1 As Integer = Ubound(arr, 1) // returns 2
Dim ub2 As Integer = Ubound(arr, 2) // returns 3
Dim ub3 As Integer = Ubound(arr, 3) // returns 4`

---

<div class="post-metadata">

**Author:** ![Eli\_Ott](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/5f8ce5/32.png) [@Eli\_Ott](https://forum.xojo.com/u/Eli_Ott)\
**Post date:** [August 24, 2015, 6:16pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/11 "2015-08-24T18:16:09Z")

</div>

There is an error in the docs:

[quote]Syntax

result=Ubound(array[,dimension])  
OR  
Introduced in 2007r2:  
result=array.Ubound(dimension)[/quote]  
The latter does not work, see this example:

```auto
Dim ub1 As Integer = arr.Ubound(1) // will not compile
```

The compiler says: This array method only works for one-dimensional arrays.

---

<div class="post-metadata">

**Author:** ![Paul\_Lefebvre](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_lefebvre/32/3152_2.png) [@Paul\_Lefebvre](https://forum.xojo.com/u/Paul_Lefebvre)\
**Post date:** [August 24, 2015, 9:11pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/12 "2015-08-24T21:11:13Z")

</div>

Wow, that has been wrong since at least 2009! I’ve updated the pages.

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [November 25, 2015, 7:31pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/13 "2015-11-25T19:31:55Z")

</div>

[quote=208832:@Gerardo García]So, then if I want to add values. Is this the right way to do it?:  
for example for the headers:

my2Darray(0,0) = “unit”  
my2Darray(0,1) = “quantity”  
my2Darray(0,2) = “Description”  
my2Darray(0,3) = “Price”

Pls tell me if I’m wrong. :)[/quote]

Hi once in a time I made a For to process a Listbox in order to pass Columns and Rows to a Multidimensional Array:  
[b] ReDim my2DArray(-1,-1) 'Por si la Matriz ya contiene datos

ReDim my2DArray(Listbox.ListCount - 1, Listbox.ColumnCount - 1) 'La redimensiona segun las columnas y filas que tenga el listbox

For i as integer=0 to listbox.listcount -1 'Cuenta las listas

```
For j as integer=0 to lista.ColumnCount -1 'Cuenta las columnas
  my2dArray(i,j) = listbox.Cell(i,j)
  'Msgbox "Matriz: " + my2dArray(i,j)
  
  
  
Next

```

Next[/b]

It works Flawlessly.

But now I have a different idea jumping on my mind.

I have a multidimensional Array, I want to have Four columns and Rows as many as I want.  
MatrizCFDI(-1,-1) As String

Then to redimension to the requested number of rows and columns I do this: ReDim MatrizCFDI(-1, 3)  
Pls. Correct me If I’m wrong.

Then In the last code I done this: my2dArray(i,j)= listbox.Cell(i,j)

But this was a Listbox. What If I have two variables to load?  
Will It be valid to use my2dArray(i,j)= “Cats”, “Dogs”??

And How will I do the For instruction talking about If I need to add four columns?  
Thanks

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [November 25, 2015, 8:13pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/14 "2015-11-25T20:13:49Z")

</div>

You can Redim with variables.

For instance :

```auto
Redim MatrizCFDI(ListBox1.rowCount, ListBox1.ColumnCount-1) As String
```

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 25, 2015, 10:02pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/15 "2015-11-25T22:02:18Z")

</div>

[quote=232008:@Gerardo García] What If I have two variables to load?  
Will It be valid to use my2dArray(i,j)= “Cats”, “Dogs”??[/quote]  
No, that is not valid. You have to set each element of the array individually.

```auto
my2dArray(i,j) = "Cats"
my2dArray(i,j+1) = "Dogs"
```

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [November 25, 2015, 10:17pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/16 "2015-11-25T22:17:46Z")

</div>

I don’t understand it. I Think Im right but I’m getting OutOfBoundsException

Countlink As Integer = 478  
ReDim MatrizCFDI(Countlink, 3)

For i as Integer = 1 to countLink -1  
MatrizCFDI(0,i) = "Lista "’ + cstr(i)

Next

I wanna write make a For and Write this value to countLink. I need to Write it on Fourth Column (Taking in count that Arrays are zero based)

---

<div class="post-metadata">

**Author:** ![Norman\_P](https://forum.xojo.com/letter_avatar_proxy/v4/letter/n/858c86/32.png) [@Norman\_P](https://forum.xojo.com/u/Norman_P)\
**Post date:** [November 25, 2015, 10:23pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/17 "2015-11-25T22:23:56Z")

</div>

ReDim MatrizCFDI(Countlink, 3) \<\<\<\<\<\<\<\<\<\<\<\< make this have rows 0 to Countlink  
MatrizCFDI(0,i) = "Lista "’ + cstr(i) \<\<\<\<\<\<\<\<\<\<\<\< this tries to set row 0 COLUMN i (which may be way above 3)

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [November 25, 2015, 10:42pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/18 "2015-11-25T22:42:39Z")

</div>

> [@232059:@Norman Palardy](#):
>
> Dim ub3 As Integer = Ubound(arr, 3)

Of course 😃😃

The right Way is: MatrizCFDI(i,3) = "Lista " + cstr(i)

At this Way Im writing always at 3th column, and the row will be updating

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [November 25, 2015, 10:45pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/19 "2015-11-25T22:45:54Z")

</div>

[quote=232059:@Norman Palardy]ReDim MatrizCFDI(Countlink, 3) \<\<\<\<\<\<\<\<\<\<\<\< make this have rows 0 to Countlink  
MatrizCFDI(0,i) = "Lista "’ + cstr(i) \<\<\<\<\<\<\<\<\<\<\<\< this tries to set row 0 COLUMN i (which may be way above 3)[/quote]  
So Now, for Catch the Value of 3th Column?

---

<div class="post-metadata">

**Author:** ![Gerardo\_Garca](https://forum.xojo.com/letter_avatar_proxy/v4/letter/g/e99b99/32.png) [@Gerardo\_Garca](https://forum.xojo.com/u/Gerardo_Garca)\
**Post date:** [November 25, 2015, 10:56pm UTC](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225/20 "2015-11-25T22:56:19Z")

</div>

I made this, but I’m making a mistake again ☹  
[b]

For z As Integer = 1 to Ubound(MatrizCFDI, 3)  
Msgbox MatrizCFDI(z,3)  
Next[/b]

[Next page](https://forum.xojo.com/t/how-to-create-a-multidimensional-array/26225.md?page=2)
