How to create a Multidimensional Array?

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

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

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=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. :slight_smile:

thats right

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

Use Ubound ().

Bound Only works for one dimensional Arrays :frowning:

It works for other dimensions
http://documentation.xojo.com/index.php/Ubound

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

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:

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

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

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

[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

You can Redim with variables.

For instance :

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

[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.

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

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)

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)

Of course :smiley: :smiley:

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

[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?

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

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