Mike R
Mike R

Reputation: 35

Two-dimensional array

I am using Visual Studio 2010 with Visual Basic. I have a dynamic 2 dimensional array that is coming into the function. I cannot figure out how to get the size of first row.

For example let say this array is passe into the function:

{{1, 0, 5, 4},
 {8, 1, 4, 4},
 {0, 1, 4, 4},
 {7, 7, 7, 4},
 {7, 7, 7, 4},
 {8, 1, 4, 4}}

In this example I would get 4 because if you look at for example first row it has 4 elements. And for example in this case:

{{1, 0, 5, 4, 7, 7},
 {8, 1, 4, 4, 7, 7},
 {0, 1, 4, 4, 8, 8},
 {7, 7, 7, 4, 3, 3},
 {7, 7, 7, 4, 4, 4},
 {8, 1, 4, 4, 1, 9}}

I would get back 6 cause rows have 6 elements in it. The array is coming in always 2d and rows are always the same length. Column size is unknown and row size in unknown. But if I find out row size for one row the all rows are that size if that makes sense.

I have tried this UBound(inArray, 1) but it does not work. Please help me figure this out.

Upvotes: 1

Views: 4521

Answers (3)

Samuel Lopes
Samuel Lopes

Reputation: 39

To find the length of the x-axis, use the GetLength function. I have tried the GetUpperBound function and sometimes it has proven to be a little unreliable. Please use the code below as reference:

Dim array(1,10) As Double
Dim x as Integer = array.GetLength(0) - 1 'Will give you value of dimension x'

Upvotes: 0

SSS
SSS

Reputation: 5393

Dim i(,) As Integer = {{1, 0, 5, 4}, {8, 1, 4, 4}, {0, 1, 4, 4}, {7, 7, 7, 4}, {7, 7, 7, 4}, {8, 1, 4, 4}}
MsgBox(i.GetUpperBound(0)) 'first index
MsgBox(i.GetUpperBound(1)) 'second index
MsgBox(UBound(i, 1)) 'first index (UBOUND is 1-based)
MsgBox(UBound(i, 2)) 'second index (UBOUND is 1-based)

For the 2D array i(x,y), GetUpperBound(0) returns the maximum value of x, and GetUpperBound(1) returns the maximum value of y

Upvotes: 1

Adriaan Stander
Adriaan Stander

Reputation: 166326

Have a look at using Array.GetUpperBound Method

Gets the upper bound of the specified dimension in the Array.

Upvotes: 2

Related Questions