vb6List Question

What is the best way to write all selected items in a list box to an array.  Here is what I have:

        
    'get races to assign to
    k = 0
    ReDim RacesToAssign(0)
    For i = 0 To lstRaces.ListCount - 1
        If lstRaces.Selected(i) = True Then
                    RacesToAssign(k) = lstRaces.Text
                    k = k + 1
                    ReDim Preserve RacesToAssign(k)
        End If
    Next i

Open in new window

Bob SchneiderCo-OwnerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertConsultingCommented:
Hi,

It all depend if you need performances or not.
Your code do the trick, but it isn't the most optimized as you constantly resize your array.
You could first make a loop to retrieve the  total selected items, wich will be your array size, thus, resizing your array only once.

Alternatively, you can use a collection instead of an array.

I woud just do a little change to you code, bc your array end with an unused item if you have more than one selected item:
        
    'get races to assign to
    k = 0
    ReDim RacesToAssign(0)
    For i = 0 To lstRaces.ListCount - 1
        If lstRaces.Selected(i) = True Then
                    If(Ubound( RacesToAssign) < k) Then
                         ReDim Preserve RacesToAssign(0 To k)
                     End If
                    RacesToAssign(k) = lstRaces.Text
                    k = k + 1
          End If
    Next i

Open in new window

Martin LissOlder than dirtCommented:
    Dim k As Long
    Dim i As Long
    Dim RacesToAssign() As String
    Dim intSelected As Integer
 'get races to assign to
'    k = 0
'    ReDim RacesToAssign(0)

    For i = 0 To lstRaces.ListCount - 1
        If lstRaces.Selected(i) = True Then
            intSelected = intSelected + 1
        End If
    Next
    ReDim RacesToAssign(intSelected - 1)
    
    For i = 0 To lstRaces.ListCount - 1
        If lstRaces.Selected(i) = True Then
                    RacesToAssign(k) = lstRaces.List(i)
                    k = k + 1
        End If
    Next i

Open in new window

A few things about the code.
  • I don't know if you define your variables or not, but you should always do that, and in addition add OPTION EXPLICIT at the top of your code.
  • Unless you previously used k and RacesTo Assign in code you are not showing, there's no need to initialize them.
  • Notice that I changed line 18 (your line 7). The 'Text' property contains only the value of the last item selected, so in your code all the entries in RacesToAssign would be the same.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange MVE 2015
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.