array declarations

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jay

    #1

    array declarations

    Why is it that array declarations are different than expected (other
    languages)

    for example:

    dim strArray(5) as String

    really means that there are 6 elements and their names are 0, 1,2,3,4,5

    I would expect the following:

    dim strArray(5) as String

    There are 5 elements and their names are 0, 1,2,3,4



  • Cor Ligthert

    #2
    Re: array declarations

    Jay,

    This is because VBNet can work with the "one" indexer and the "zero"
    indexer. In my opininion are there as well other solutions for that, however
    they did it like this consequently keeping the indexer 1 and not internally
    set in those VBNet methods the one by -1 by instance.

    I hope this gives an idea?

    Cor

    "Jay" <
    ...[color=blue]
    > Why is it that array declarations are different than expected (other
    > languages)
    >
    > for example:
    >
    > dim strArray(5) as String
    >
    > really means that there are 6 elements and their names are 0, 1,2,3,4,5
    >
    > I would expect the following:
    >
    > dim strArray(5) as String
    >
    > There are 5 elements and their names are 0, 1,2,3,4
    >
    >
    >[/color]


    Comment

    • Larry Serflaten

      #3
      Re: array declarations


      "Jay" <j@h.com> wrote
      [color=blue]
      > Why is it that array declarations are different than expected (other
      > languages)
      >
      > for example:
      >
      > dim strArray(5) as String
      >
      > really means that there are 6 elements and their names are 0, 1,2,3,4,5[/color]


      Probably because VB used to allow declaring the lower bound:

      Dim strA(10 To 20) As String

      (You'll note that there are 11 elements there) VB also used to have an
      Option Base such that either 0 or 1 could be used as the default:

      Option Base 1
      Dim strB(5) As String

      In that case there were 5 elements : 1, 2, 3, 4, 5

      Option Base 0
      Dim strB(5) As String ' 6 elements there

      Now because .Net's arrays are by default 0 based, then it should
      follow that they mimic the old Option Base 0 functionality.. ..

      LFS

      Comment

      • Herfried K. Wagner [MVP]

        #4
        Re: array declarations

        "Jay" <j@h.com> schrieb:[color=blue]
        > Why is it that array declarations are different than expected
        > (other languages)
        >
        > for example:
        >
        > dim strArray(5) as String
        >
        > really means that there are 6 elements and their names are 0, 1,2,3,4,5
        >
        > I would expect the following:
        >
        > dim strArray(5) as String
        >
        > There are 5 elements and their names are 0, 1,2,3,4[/color]

        In Addition to the other replies: VB 2005 will support the 'Bla(0 To 100)'
        syntax again, but only with zero bounds. I'll update my code to this syntax
        when VB 2005 comes out to avoid ambiguity for the reader.

        --
        Herfried K. Wagner [MVP]
        <URL:http://dotnet.mvps.org/>

        Comment

        • Rangi Keen

          #5
          Re: array declarations

          If you look at the documentation for VB.NET array declarations they state
          that the number is the upper bound, not the length. The array is 0-based and
          the Length property will return 6 in your example. The GetUpperBound method
          will return 5.

          ArraySizeInitia lizationModifie r ::=
          ( UpperBoundList ) [ ArrayTypeModifi ers ]
          UpperBoundList: :=
          Expression |
          UpperBoundList , Expression

          "Jay" <j@h.com> wrote in message news:VFIad.2921 $EZ.579@okeprea d07...[color=blue]
          > Why is it that array declarations are different than expected (other
          > languages)
          >
          > for example:
          >
          > dim strArray(5) as String
          >
          > really means that there are 6 elements and their names are 0, 1,2,3,4,5
          >
          > I would expect the following:
          >
          > dim strArray(5) as String
          >
          > There are 5 elements and their names are 0, 1,2,3,4
          >
          >
          >[/color]


          Comment

          Working...