I am writing a code in which I have multi-dimensional arrays. I need to know programmatically

1. Dimensions of the array

2. Size of each dimension

I am using dynamic arrays so I cannot know about the dimensions or size of the array in advance.

I can get the dimensions using pointers with CopyMemory function, but my problem is taht I also need to know the size of all dimensions i.e. if array is two dimensional then UBOUND function gives the size of one dimension only what about the size of second dimension

e.g

arr(3,4)

UBOUND(arr) gives '3' but I also need '4' programmatically.

## 2 Replies - 14883 Views - Last Post: 12 January 2008 - 01:44 AM

##
**Replies To:** Array Size & Dimensions

### #2

## Re: Array Size & Dimensions

Posted 11 January 2008 - 11:48 PM

Are you using VB6 or VB.Net (VB 2005, VB 2005 Express are VB.Net)

### #6

## Re: Array Size & Dimensions

Posted 12 January 2008 - 01:44 AM

To determine the size and dimensions of the array, first you need to find the length using the

Then take the Length and divide it by the index number returned by UBOUND and you will have the size of the second dimension. Don't forget to add one to the value returned by UBOUND because it only returns the highest index number, not the actual number of elements. And then subtract one to get the value back to the highest index number of the second dimension.

Example:

The result in the MsgBox will be 3, 4

Keep in mind when declaring arrays in VB that you are specifying the highest index number, not the actual number of elements. So an array of 3 by 4 will actually have 20 elements.

First dimension indexes will be 0 - 3 for a total of 4 elements and second dimension indexes will have 0 - 4 for a total of 5 elements.

*array.Length*. Now that you have the total number of elements of both dimensions you can use UBOUND to determine the highest index number of the first dimension.Then take the Length and divide it by the index number returned by UBOUND and you will have the size of the second dimension. Don't forget to add one to the value returned by UBOUND because it only returns the highest index number, not the actual number of elements. And then subtract one to get the value back to the highest index number of the second dimension.

Example:

Dim arr(3, 4) As Integer Dim firstDim As Integer = UBound(arr) Dim secDim As Integer = CInt((arr.Length / (UBound(arr) + 1)) - 1) MsgBox((firstDim & ", " & secDim))

The result in the MsgBox will be 3, 4

Keep in mind when declaring arrays in VB that you are specifying the highest index number, not the actual number of elements. So an array of 3 by 4 will actually have 20 elements.

First dimension indexes will be 0 - 3 for a total of 4 elements and second dimension indexes will have 0 - 4 for a total of 5 elements.

Page 1 of 1