0 based collections

B

Boni

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
 
C

Cor Ligthert [MVP]

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
 
H

Herfried K. Wagner [MVP]

Boni said:
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.

Yes (see below).
Is
dim col as collection
col alwasy 1 based?
Yes.

Why the whole theater? Why not have all stuff 0 based?

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.Collections' namespace, in particular 'Hashtable' and 'ArrayList'.
 
B

Boni

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.Collections' namespace, in particular 'Hashtable' and 'ArrayList'.
ok. Thanks.
How is it done in C#? Is System.collection also starting with 1 there?
 
H

Herfried K. Wagner [MVP]

Boni said:
How is it done in C#? Is System.collection also starting with 1 there?

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

Mick Doherty

Microsoft.VisualBasic.Collection 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.Collections
namespace instead. I believe they are better optimised than the
Microsoft.VisualBasic.Collection.
 

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