How to get the size of an array?

S

Shapper

Hello,

I have this for loop:

For i = 1 to 10
....
Next

I want to use the size of an array instead of 10.

How to determine the size of an array?

Thanks,
Miguel
 
M

Mythran

Shapper said:
Hello,

I have this for loop:

For i = 1 to 10
...
Next

I want to use the size of an array instead of 10.

How to determine the size of an array?

Thanks,
Miguel

Dim myArray As String() = New String() { "This", "is", "a", "string",
"array" }

For i = 0 To myArray.GetUpperBounds(0)
' Do something.
Next

NOTE: Can't remember if GetUpperBounds returns a 0 based index...it
probably does so you may need myArray.GetUpperBounds(0) - 1 instead.

Mythran
 
C

Craig Deelsnyder

Shapper said:
Hello,

I have this for loop:

For i = 1 to 10
...
Next

I want to use the size of an array instead of 10.

How to determine the size of an array?

Thanks,
Miguel

myArray.Length
 
E

Egghead

When you use the ubound(array) in VB.net, it gives you the upper index of
the array, nothing to do with Zero based. such as
dim a as string() = {"A","B","C","D","E"}
ubound(a) will give you 4, same as in VB 6.0. Therefore, just to know the
size, it is better to use the length. It gives you 5. Just remember to "-1"
when you use it in a loop.

Do you get your mcsd in C#?

Egghead
 
S

Steve C. Orr [MVP, MCSD]

You are right.
I was confusing it with Array.Length, in which case the "-1" should be used.
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top