Counting items in an array?

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
 
T

Teemu Keiski

GetLength expects the dimension as parameter (count of items in a
dimension). Did you try just Length property of the array itself?
 
W

wtsnet

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
 
H

Hans Kesting

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
 
R

Rob Meade

...

[..snip..]

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

Regards

Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top