Click to See Complete Forum and Search --> : array


Chris B.
January 31st, 2000, 08:24 AM
If I have an array that looks something like this.

Dim UserArray as Variant
UserArray = Array ("me", "you", "and him")



Can I add all three entries to a combobox at the same time? So instead of this:

mycontrol.AddItem (UserArray(0))
mycontrol.AddItem (UserArray(1))
mycontrol.AddItem (UserArray(2))



It would look more like this:

mycontrol.AddItem (UserArray(0 - 2))



Is something liek this possible? Thanks...

ncl
January 31st, 2000, 10:06 AM
Wrap it in a loop

for x = 0 to ubound(userarray)
mycontrol.additem userarray(x)
next

xxMariusxx
January 31st, 2000, 11:15 AM
also, remember that VB for the most part is 1-based and not Zero-based like C or any of its derivatives. Usearray(0) would return a "subscript out of range" error or "index out of bounds" or something to that effect.

Chris Eastwood
February 1st, 2000, 05:02 AM
I wish VB were 0 or 1 based - there's so much inconsistancy throughout vb it's frustrating at times.

The code given would work fine - The basing of arrays depends on the :


option Base n




- statement being in your code (where n is 0, 1 etc).

For example : The following code works fine with 'Option Base 0' (or ommitted):


Dim vArray as Variant
'
vArray = Array("Chris", "Was", "Here")
'
Dim i as Integer
'
for i = 0 to UBound(vArray)
MsgBox vArray(i)
next
'




However, even this is bad practise as it assumes that the array will always start at '0' as it's base - the correct way is to use the LBound function along with the UBound function, ie:


for i = LBound(vArray) to UBound(vArray)
MsgBox vArray(i)
next




- That way, anyone hacking around in your code with 'Option Base...' won't affect the way that the code works.


Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb