Arrays with length of 0

Discussion in 'Microsoft VB .NET' started by Guest, Jan 2, 2006.

  1. Guest

    Guest Guest

    Hi,

    Using VB 2005, I have a function that returns an array of a certain object.
    In certain cases the function should return an array with a length of 0.
    I've found that this works:

    Dim child(0) As Object
    Array.Resize(Of Object)(child, 0)
    Return child

    If I leave out the middle line, it returns an array with a length of 1, the
    object in the array being nothing.

    I'm interested in knowing why this is the case. Why doesn't "Dim child(0)
    as Object" create an array with a length of 0?

    Thanks,
    Nathan
     
    Guest, Jan 2, 2006
    #1
    1. Advertisements

  2. "Nathan M" <> schrieb:
    > Using VB 2005, I have a function that returns an array of a certain
    > object.
    > In certain cases the function should return an array with a length of 0.
    > I've found that this works:
    >
    > Dim child(0) As Object
    > Array.Resize(Of Object)(child, 0)
    > Return child
    >
    > If I leave out the middle line, it returns an array with a length of 1,
    > the
    > object in the array being nothing.


    \\\
    Dim child(-1) As Object
    ///

    > I'm interested in knowing why this is the case. Why doesn't "Dim child(0)
    > as Object" create an array with a length of 0?


    'Dim <variablename>(<upper bound>) As <type>'. The resulting array will
    have <upper bound> + 1 elements, i.e. 'Dim child(10) As Object' will
    construct an array with 11 elements with indices 0, ..., 10.

    --
    M S Herfried K. Wagner
    M V P <URL:http://dotnet.mvps.org/>
    V B <URL:http://classicvb.org/petition/>
     
    Herfried K. Wagner [MVP], Jan 2, 2006
    #2
  3. Nathan,

    The why
    >
    > Dim child(0) As Object
    > Array.Resize(Of Object)(child, 0)
    > Return child
    >
    > I'm interested in knowing why this is the case. Why doesn't "Dim child(0)
    > as Object" create an array with a length of 0?
    >

    A strange way that is chosen to keep VBNet compatible with the methods which
    uses First as start index instead of Zero. (It would been great as they had
    chosen for one of those, than methods as Mid and Len would not be so
    confusing).

    However, done is done and you can not make those decisions not done.

    Cor
     
    Cor Ligthert [MVP], Jan 2, 2006
    #3
  4. Guest

    Guest

    Dim child(0) As Object
    means the same as the old vb6 style
    Dim child(0 To 0) As Object

    Use
    Dim child(-1) As Object for 0 length
     
    , Jan 2, 2006
    #4

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. Tim Eaden

    arrays in vb.net !

    Tim Eaden, Jul 9, 2003, in forum: Microsoft VB .NET
    Replies:
    3
    Views:
    530
    Armin Zingler
    Jul 9, 2003
  2. active
    Replies:
    1
    Views:
    494
    Alexandre Moura
    Nov 21, 2003
  3. Brian Black
    Replies:
    0
    Views:
    337
    Brian Black
    Jan 13, 2005
  4. Rick Knospler
    Replies:
    1
    Views:
    548
    Ryan S. Thiele
    Feb 16, 2007
  5. Demosthenes
    Replies:
    5
    Views:
    692
    Patrice
    Jan 23, 2008
Loading...

Share This Page