Array Size & Dimensions

Array Dimensions and size of each dimension

Page 1 of 1

2 Replies - 16804 Views - Last Post: 12 January 2008 - 01:44 AM Rate Topic: -----

#1 kmunir  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 11-January 08

Array Size & Dimensions

Posted 11 January 2008 - 11:36 PM

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
UBOUND(arr) gives '3' but I also need '4' programmatically.

Is This A Good Question/Topic? 0
  • +

Replies To: Array Size & Dimensions

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1659
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

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)
Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

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

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.
Was This Post Helpful? 1

Page 1 of 1