Arrays with length of 0

G

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
 
H

Herfried K. Wagner [MVP]

Nathan M said:
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.
 
C

Cor Ligthert [MVP]

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
 
D

doker0

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top