Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

0-indexing and Visual Basic

Status
Not open for further replies.

Quimbly

Programmer
Oct 24, 2002
33
CA
I come from the C-code world, where everything is 0-indexed: arrays, strings, DB records, etc.

I've seen VB code that starts indexes collection objects from both 0 and 1. Which is it?! Or is it different from object to object?

For instance, the first character in a string object is numbered "1" -- is this correct?

How about for arrays?

What else?!
 
An array is zero based
dim MyArray(5) as string
has 6 elements beginning with MyArray(0) to Myarray(5).

In the mid function, that I beleive you are talking about, the second parameter is the starting location in the string. so the first character would be 1. Hope this helps.

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
The default lower bound for an array is zero. You can use the :[tt]

Option Base 1
[/tt]
statement at module level to set all array lower bounds to 1

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 

Arrays can be 0 or 1 based depending upon the coder UNLESS via the split function. Meaning you can have your arrays set to a default of 1 if you wish by including this line "Option Base 1" under the "Option Explicit" line UNLESS an array is specifically dimentioned as variable or zero based...
[tt]
Option Explicit
Option Base 1

Dim Ar1(5) As String 'this array is from 1 to 5
Dim Ar2(0 To 5) As String 'this array is from 0 to 5
Dim Ar3() As String
Dim Ar4(100 To 101) 'contains two elements

Private Sub Form_Load()
Ar3 = Split("This is a test", " ") 'this array will contain elements from 0 to 3
End Sub
[/tt]

Then in general lists are zero based (as in a list boxes list) but items are 1 (as in a collection or a list boxes count property) based no matter what option base you attempt to use.

Good Luck

 
Most prominently as I think of 1-based items right now is the ListView control and Collection objects. Dynamic arrays, listboxes, comboboxes, control arrays are all handled with a base of 0.

If you're not sure, you can open up a blank VB form, drop the desired control onto it and whip up some sample code to test whether or not 0 is the lower boundary by default. You'll get a subscript error if it's not.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top