Welcome to Dream.In.Code
Become a VB Expert!

Join 149,951 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,324 people online right now. Registration is fast and FREE... Join Now!




Array Size & Dimensions

 
Reply to this topicStart new topic

Array Size & Dimensions, Array Dimensions and size of each dimension

kmunir
11 Jan, 2008 - 10:36 PM
Post #1

New D.I.C Head
*

Joined: 11 Jan, 2008
Posts: 1

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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Array Size & Dimensions
11 Jan, 2008 - 10:48 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Are you using VB6 or VB.Net (VB 2005, VB 2005 Express are VB.Net)
User is offlineProfile CardPM
+Quote Post

Jayman
RE: Array Size & Dimensions
12 Jan, 2008 - 12:44 AM
Post #3

Student of Life
Group Icon

Joined: 26 Dec, 2005
Posts: 7,317



Thanked: 66 times
Dream Kudos: 500
Expert In: Everything

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

Example:
CODE

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 05:42PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month