Avatar of josephdaviskcrm
josephdaviskcrmFlag for United States of America

asked on 

VB.NET - Find dimensions of a two dimensional array

Given a two dimensional array of varying size on either dimension.  How do I find the number of elements in each dimension?  I know for a one dimensional array I can use GetUpperBound and GetLowerBound.  But how can that be applied to a two dimensional array?
Visual Basic.NET

Avatar of undefined
Last Comment
Fernando Soto
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

Actually, in VB.Net there is no such thing as a lower bound.  It's always 0, and you should use .Length() instead of .GetUpperBound().

Also, in .Net you can have jagged arrays, so while one dimension is consistent, each element of that dimension can have it's own bound.  So why don't you share where the array is coming from.
ASKER CERTIFIED SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of josephdaviskcrm

ASKER

They're actually being freshly created.  So I have a different question that may more directly solve my probelm.  When an array is declared, what is its contents before it is initialized by specific data?  Does it automatically fill itself with zeros?  Or will there be an error if the program tries to use the data in one of the elements of the array before it is initialized with a value?
Avatar of Priest04
Priest04
Flag of Serbia image

It depends on how is array declared. if you declare an array of ints, all elements are initialized to zero.
Avatar of josephdaviskcrm

ASKER

Thanks!
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi josephdaviskcrm;

Correcting some errors on this thread. If we define an array as in the following line:

        Dim test(4, 6) As Integer

Then to get the number of elements in each dimension 0 and 1 you can do the following.
 
        Dim d0 As Integer = test.GetUpperBound(0)  ' returns 5
        Dim d1 As Integer = test.GetUpperBound(1)  ' returns 7

Note that the number of elements in the dimension is one more then what was defined when you created the object. This is because all arrays start at zero and the  dimension values there define the upper index limit of that dimension.

To get the total number of elements in the complete array use it length property as shown below.

        Dim dTotal As Integer = test.Length                 ' returns 35

Fernando
Avatar of Priest04
Priest04
Flag of Serbia image

You are welcome, josephdaviskcrm.

FernandoSoto, I dont see what errors were you referring to. In c# when you declare an array, you actually put the number of elements the array will hold. So,

int[,] test = new int[4, 6];

will have 4 elements in first dimension (index 0 - 3) and 6 elements in 2nd dimension (index 0-5). And GetUpperBound() returns highest index, not the number of elements, so it will return 3 for 1st dimension, and 5 for 2nd dimension.

So, there were no errors.

Goran
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Hi Priest04;

First the question was asked in the Microsoft Visual Basic thread. Most of the time posting C# solutions is not an issue but the author of the question posted as a beginner in this subject and in some areas the two language do differ. As shown in the sample code below. It is true that in C# the way you define the array is correct but the meaning in Visual Basic .Net does not hold true. As you correctly stated, "In c# when you declare an array, you actually put the number of elements the array will hold." This is not correct in Visual Basic .Net, in VB.Net the indexes are the max index of the particular dimension. Therefore in the following lines of code the C# version has 24 total elements where as the VB .Net version has 35 elements.

int[,] test = new int[4, 6];        //  Array holds 24 elements
Dim test(4, 6) As Integer     '    Array holds 35 elements

Fernando

In C# Code:
 
// On the next line of code the index 4 and 6 represent the total number of elements in each dimension.
int[,] test = new int[4, 6];
 
int d0 = test.GetUpperBound(0); // returns 3 meaning index range 0 - 3 or 4 elements for the dimension
int d1 = test.GetUpperBound(1); // returns 5 meaning index range 0 - 5 or 6 elements for the dimension
int dTotal = test.Length;       // 24 for a total number of 24 element in the array
 
In Visual Basic .Net:
 
' On the next line of code the index 4 and 6 represent the highest
  index for the dimension and not the number of elements in the dimension.
Dim test(4, 6) As Integer
 
Dim d0 As Integer = test.GetUpperBound(0)  ' returns 4 meaning index range 0 - 4 or 5 elements for the deminsion
Dim d1 As Integer = test.GetUpperBound(1)  ' returns 6 meaning index range 0 - 6 or 7 elements for the deminsion
Dim dTotal As Integer = test.Length        ' 35 for a total number of 35 element in the array

Open in new window

Visual Basic.NET
Visual Basic.NET

Visual Basic .NET (VB.NET) is an object-oriented programming language implemented on the .NET framework, but also supported on other platforms such as Mono and Silverlight. Microsoft launched VB.NET as the successor to the Visual Basic language. Though it is similar in syntax to Visual Basic pre-2002, it is not the same technology,

96K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo