How to get the size of an array?

Discussion in 'ASP .Net' started by Shapper, May 25, 2005.

  1. Shapper

    Shapper Guest

    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, May 25, 2005
    #1
    1. Advertising

  2. Shapper

    Mythran Guest

    "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    news:...
    > 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
     
    Mythran, May 25, 2005
    #2
    1. Advertising

  3. For i = 0 to UBound(MyArray)-1
    ....
    Next

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


    "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    news:...
    > 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
    >
     
    Steve C. Orr [MVP, MCSD], May 25, 2005
    #3
  4. Shapper

    Egghead Guest

    hmmm, I think "Ubound" no need to -1

    Egghead
    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:...
    > For i = 0 to UBound(MyArray)-1
    > ...
    > Next
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://SteveOrr.net
    >
    >
    > "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    > news:...
    > > 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
    > >

    >
    >
     
    Egghead, May 25, 2005
    #4
  5. Shapper wrote:
    > 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

    --
    Craig Deelsnyder
    Microsoft MVP - ASP/ASP.NET
     
    Craig Deelsnyder, May 25, 2005
    #5
  6. I think you do need it.
    it's zero based.

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


    "Egghead" <> wrote in message
    news:...
    > hmmm, I think "Ubound" no need to -1
    >
    > Egghead
    > "Steve C. Orr [MVP, MCSD]" <> wrote in message
    > news:...
    >> For i = 0 to UBound(MyArray)-1
    >> ...
    >> Next
    >>
    >> --
    >> I hope this helps,
    >> Steve C. Orr, MCSD, MVP
    >> http://SteveOrr.net
    >>
    >>
    >> "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    >> news:...
    >> > 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
    >> >

    >>
    >>

    >
    >
     
    Steve C. Orr [MVP, MCSD], May 25, 2005
    #6
  7. Shapper

    Egghead Guest

    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

    "Steve C. Orr [MVP, MCSD]" <> wrote in message
    news:%...
    > I think you do need it.
    > it's zero based.
    >
    > --
    > I hope this helps,
    > Steve C. Orr, MCSD, MVP
    > http://SteveOrr.net
    >
    >
    > "Egghead" <> wrote in message
    > news:...
    > > hmmm, I think "Ubound" no need to -1
    > >
    > > Egghead
    > > "Steve C. Orr [MVP, MCSD]" <> wrote in message
    > > news:...
    > >> For i = 0 to UBound(MyArray)-1
    > >> ...
    > >> Next
    > >>
    > >> --
    > >> I hope this helps,
    > >> Steve C. Orr, MCSD, MVP
    > >> http://SteveOrr.net
    > >>
    > >>
    > >> "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    > >> news:...
    > >> > 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
    > >> >
    > >>
    > >>

    > >
    > >

    >
    >
     
    Egghead, May 25, 2005
    #7
  8. 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


    "Egghead" <> wrote in message
    news:%23Osy4%...
    > 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
    >
    > "Steve C. Orr [MVP, MCSD]" <> wrote in message
    > news:%...
    >> I think you do need it.
    >> it's zero based.
    >>
    >> --
    >> I hope this helps,
    >> Steve C. Orr, MCSD, MVP
    >> http://SteveOrr.net
    >>
    >>
    >> "Egghead" <> wrote in message
    >> news:...
    >> > hmmm, I think "Ubound" no need to -1
    >> >
    >> > Egghead
    >> > "Steve C. Orr [MVP, MCSD]" <> wrote in message
    >> > news:...
    >> >> For i = 0 to UBound(MyArray)-1
    >> >> ...
    >> >> Next
    >> >>
    >> >> --
    >> >> I hope this helps,
    >> >> Steve C. Orr, MCSD, MVP
    >> >> http://SteveOrr.net
    >> >>
    >> >>
    >> >> "Shapper" <mdmoura*NOSPAM*@gmail.*DELETE2SEND*com> wrote in message
    >> >> news:...
    >> >> > 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
    >> >> >
    >> >>
    >> >>
    >> >
    >> >

    >>
    >>

    >
    >
     
    Steve C. Orr [MVP, MCSD], May 25, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Zygmunt Krynicki
    Replies:
    1
    Views:
    638
    Ivan Vecerina
    Oct 11, 2003
  2. Replies:
    5
    Views:
    568
    Flash Gordon
    Apr 9, 2006
  3. Replies:
    8
    Views:
    520
    Bob Hairgrove
    Apr 10, 2006
  4. Jason Cavett

    Preferred Size, Minimum Size, Size

    Jason Cavett, May 23, 2008, in forum: Java
    Replies:
    5
    Views:
    12,704
    Michael Jung
    May 25, 2008
  5. Pallav singh
    Replies:
    2
    Views:
    311
    Bo Persson
    Apr 7, 2009
Loading...

Share This Page