CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: array

  1. #1
    Join Date
    Jun 1999
    Location
    Texas
    Posts
    119

    array

    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...


  2. #2
    Join Date
    Jan 2000
    Posts
    8

    Re: array

    Wrap it in a loop

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


  3. #3
    Join Date
    Jan 2000
    Posts
    20

    Re: array

    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.


  4. #4
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: array

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured