Avatar of Tom_Hickerson
Tom_Hickerson
Flag for United States of America asked on

Using arrays

If I want to have an array of strings that I can add to.  How can I do this without keeping track of every time I add a new string.  

So what I have been doing is this...
  Dim myStrings(1000) As String
  Dim myStringCount As Integer


Then while the program is doing its sorting work
   If Not myStrings.Contains(pair.Primary) Then
                myStrings(myStringCount) = pair.Primary
              myStringCount  = myStringCount + 1
    End If

Then when I display them
I do a for loop using myStringCount to display each item
     For i = 0 To myStringCount - 1
            ListBox1.Items.Add(MyStrings(i))
        Next



I'm sure there is a more efficient way.  How would you guys do this?
Visual Basic.NET

Avatar of undefined
Last Comment
Neil Fleming

8/22/2022 - Mon
Jayadev Nair

Are you looking for -

For Each item As String In myStrings
    ListBox1.Items.Add(item)
Next

Open in new window

Tom_Hickerson

ASKER
Thats what I would like to be able to use, but I think because of how I am doing it with the myStringCount it does not work.

I get a null exception if I try and use that.
ASKER CERTIFIED SOLUTION
Jayadev Nair

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Tom_Hickerson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
SOLUTION
Neil Fleming

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Tom_Hickerson

ASKER
@Neil

The end goal is to use the items in the list to create a check boxes at run time that can be selected to show the data from that pair.  Would you still do it with the single csv string?

Thanks for the input.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Neil Fleming

I think it depends partly on how many items there are going to be in your list. If it's <30 I'd use the single csv string approach because then you have no need to track how many items there are.

If it's a very large number it may be better to reDim Preserve the array as you go along. I guess you are creating checkboxes on the fly, in which case you will need to iterate the array as in your original example.