How to get the size of an array?

  • Thread starter Thread starter Shapper
  • Start date Start date
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
 
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
 
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
 
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
 
You are right.
I was confusing it with Array.Length, in which case the "-1" should be used.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net
 

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

Similar Threads

Tablet size: I love the 8 inches 1
Array 1
Windows 10 One of my previous builds is experiencing random, multiple BSODs 9
Shape Arrays VBA 0
Array 1
Enum to Array Of String 1
How to add controls to Controls.Collection? 1
Array 4

Back
Top