Counting items in an array?

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

Ok - I appreciated I could probably just do this :

For Each item IN array

intCount = intCount + 1

Next

I figured there might be a better way in .net...

I stumbled across this :

array.GetLength(intCount)

etc

I have tried using it but it returns as zero each time - in my current
scenario there are 2 items in the array...

The code I am using is such :

aReturnCodes = Split(strReturnCode, ",") ' populates array
aReturnCodes.GetLength(intArrayItems) ' gets length of array
label3.text = intArrayItems ' displays to page

My string has the following text before it goes into the array

6,7,

Therefore I expect at least 2 elements in my array - quite possibly 3
because of the trailing comma (to be removed)

However the label displays 0 ??

Any ideas?

Regards
Rob
 
GetLength expects the dimension as parameter (count of items in a
dimension). Did you try just Length property of the array itself?
 
You probably want something like:

intArrayItems=aReturnCodes.Length which gives the total
number of elements in the array. As you've got a 1
dimensional array, this is fine.

GetLength returns the length of the array in a specific
dimension of that array (say you've got a 2 by 3 array,
Array.GetLength(0) might return '2' and Array.GetLength
(1) might return '3'

Think I've got that right... give it a whirl
 
Rob Meade said:
Hi all,

Ok - I appreciated I could probably just do this :

For Each item IN array

intCount = intCount + 1

Next

I figured there might be a better way in .net...

I stumbled across this :

array.GetLength(intCount)

array.GetLength does not return it's value in the "intCount" parameter you
supply.

for a single-dimension array, you can use array.Length, which gives the
total number of
elements in the array.
For a multi-dimension array, you can use array.GetLength(dimension) to get
the number
of elements in *that* dimension.

Hans Kesting
 
...

[..snip..]

Found out I was doing it wrong - the integer in brackets was supposed to
refer to the column in the array...

Regards

Rob
 
Back
Top