0 based/1 based

rbulph

Junior Contributor
Joined
Feb 17, 2003
Messages
397
Is it true to say that every type of grouping class (collection, array, hashtable etc.) is zero based in VB.net, except for the collection class which is 1 based? I note in particular that the IList contained in the List property of CollectionBase is 0 based. I find this a bit annoying. If I have a collection and then decide I need to make it strongly typed by having a class inheriting from CollectionBase I either have to change all item references in my code or have the following rather awkward code in the new class:

Visual Basic:
   Public Function item(ByVal i As Integer) As String
        Return MyBase.List.Item(i + 1)
    End Function

Any thoughts?
 
The Collection class in VB is a visual basic specific class (it is in the Microsoft.VisualBasic namespace) and is probably there for the comfort factor alone. All of the other "grouping" classes are part of the larger .Net Framework, which all .Net languages use, and which specifies that array types are 0 based.
You should probably get used to using the 0 based classes, and thus instead of using the code above, just make changes when using the collection class.
 
Last edited:
1-based collections are an oddity mostly unique to VB. As far as I can remember all other version of Basic and all other languages I have ever used have 0-based arrays and collections, and in the end it is simpler and more logical. At the moment you might not agree with that, but maybe once you get used to it you will appreciate it.
 
I certainly think that consistency across classes is the best thing. I may well stop using the Collection class since it's a pain to have to keep thinking "Now is this a zero based or 1 based index?". If it's always zero then that is a lot easier for me.
 
You will probably find the classes under System.Collections for more useful and functional than the VB Collection class anyway. Plus if you are using .Net 2 the System.Collections.Generic namespace is a dream come true.
 
It's too bad they didn't make a few more classes 1-based alongside their 0-based brethren. I love those mix-and-match base indexes!

Oh wait, I hated those in VB6! Go go dot net gadget!

-nerseus

PS I'm just in a chipper mood today. And not a Fargo-like chipper either.
 
Back
Top