1

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.

3 Answers 3

2

Have a look at using Array.GetUpperBound Method

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

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

1 Comment

MsgBox("Amount of elements in a row " + inArray.GetUpperBound(1).ToString) that worked Thanks:) I guess I need to take a break. :)
1
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

Comments

0

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'

Comments

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.