Groups | Blog | Home
all groups > asp.net > may 2005 >

asp.net : How to get the size of an array?



Mythran
5/24/2005 4:50:11 PM

[quoted text, click to view]

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
Steve C. Orr [MVP, MCSD]
5/24/2005 7:41:46 PM
For i = 0 to UBound(MyArray)-1
....
Next

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


[quoted text, click to view]

Egghead
5/24/2005 10:20:51 PM
hmmm, I think "Ubound" no need to -1

Egghead
[quoted text, click to view]

Shapper
5/24/2005 11:27:48 PM
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
Craig Deelsnyder
5/25/2005 12:24:13 AM
[quoted text, click to view]

myArray.Length

--
Craig Deelsnyder
Steve C. Orr [MVP, MCSD]
5/25/2005 1:22:20 AM
I think you do need it.
it's zero based.

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


[quoted text, click to view]

Egghead
5/25/2005 9:41:12 AM
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

[quoted text, click to view]

Steve C. Orr [MVP, MCSD]
5/25/2005 1:20:17 PM
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


[quoted text, click to view]

AddThis Social Bookmark Button