0 based collections

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

    #1

    0 based collections

    Dear all,
    it is very confusing for me, some elements in VB are 0-base, i.e. arrays
    others are 1 -based.
    Is it possible to have 0 based .NET collections.
    Is
    dim col as collection
    col alwasy 1 based?
    Why the whole theater? Why not have all stuff 0 based?
    Thanks,
    Boni


  • Cor Ligthert [MVP]

    #2
    Re: 0 based collections

    Boni,

    Visual Basic has in my opinion taken the right approach that counting start
    at First. In my own natural language there is not even an equivalent for
    Zero other than Null (nothing).

    However, it seems that developers (like me) want to stick on machine code
    and use therefore the Zero as first counting position. (Counting starting in
    most current cultures with our fingers, from which we have normally when we
    are born ten).

    However, almost every collection and array in VBNet starts at Zero, with the
    exception with the one, which you should in my opinion avoid, the
    VB.Collection. If you don't need that for a kind of upgrading than don't use
    it. (Although it is an official Net element not a compatible element).

    A in my personal opinion strange behaviour from VBNet is, that when you
    create an array (and only with that). There is created one extra element.
    And therefore when you want an array of 1 element you have to say.

    dim mystring(0) as String ' Which is an array of 1 elements.

    (It is for those who want to use the "First" element as starting indexer).

    I hope this gives an idea.

    Cor


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: 0 based collections

      "Boni" <oilia@nospam > schrieb:[color=blue]
      > it is very confusing for me, some elements in VB are 0-base, i.e. arrays
      > others are 1 -based.
      > Is it possible to have 0 based .NET collections.[/color]

      Yes (see below).
      [color=blue]
      > Is
      > dim col as collection
      > col alwasy 1 based?[/color]

      Yes.
      [color=blue]
      > Why the whole theater? Why not have all stuff 0 based?[/color]

      The 'Collection' class works similar to the VB6 'Collection' class. If you
      want a 0-based collection, take a look at the classes in the
      'System.Collect ions' namespace, in particular 'Hashtable' and 'ArrayList'.

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      • Boni

        #4
        Re: 0 based collections

        > The 'Collection' class works similar to the VB6 'Collection' class. If[color=blue]
        > you want a 0-based collection, take a look at the classes in the
        > 'System.Collect ions' namespace, in particular 'Hashtable' and 'ArrayList'.[/color]
        ok. Thanks.
        How is it done in C#? Is System.collecti on also starting with 1 there?


        Comment

        • Herfried K. Wagner [MVP]

          #5
          Re: 0 based collections

          "Boni" <oilia@nospam > schrieb:[color=blue][color=green]
          >> The 'Collection' class works similar to the VB6 'Collection' class. If
          >> you want a 0-based collection, take a look at the classes in the
          >> 'System.Collect ions' namespace, in particular 'Hashtable' and
          >> 'ArrayList'.[/color]
          >
          > How is it done in C#? Is System.collecti on also starting with 1 there?[/color]

          The collection classes in 'System.Collect ions' are /always/ 0-based, VB's
          own collection is /always/ 1-based.

          --
          M S Herfried K. Wagner
          M V P <URL:http://dotnet.mvps.org/>
          V B <URL:http://classicvb.org/petition/>

          Comment

          • Mick Doherty

            #6
            Re: 0 based collections

            Microsoft.Visua lBasic.Collecti on is not normally used in C#.
            This has been added for VBClassic migrators who were used to using VB's 1
            based collection object.

            Try to use one of the collection classes from the System.Collecti ons
            namespace instead. I believe they are better optimised than the
            Microsoft.Visua lBasic.Collecti on.

            --
            Mick Doherty
            Dotnetrix offers Nothing. Standard and Premium versions available.



            "Boni" <oilia@nospam > wrote in message
            news:uwCeU$ugFH A.3448@TK2MSFTN GP12.phx.gbl...[color=blue][color=green]
            >> The 'Collection' class works similar to the VB6 'Collection' class. If
            >> you want a 0-based collection, take a look at the classes in the
            >> 'System.Collect ions' namespace, in particular 'Hashtable' and
            >> 'ArrayList'.[/color]
            > ok. Thanks.
            > How is it done in C#? Is System.collecti on also starting with 1 there?
            >[/color]


            Comment

            Working...