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?
LVL 1
Tom_HickersonAsked:
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.

Jayadev NairApplication DeveloperCommented:
Are you looking for -

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

Open in new window

0
Tom_HickersonAuthor Commented:
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.
0
Jayadev NairApplication DeveloperCommented:
Ok, Use this way -

Dim myStrings As New List(Of String)()

 If Not myStrings.Contains(pair.Primary) Then
                myStrings.Add(pair.Primary)
  End If

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

Open in new window


If you need this as array at some point, use this -

Dim myArray As String() = myStrings.ToArray()

and for count -

myStrings.Count
0

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
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Tom_HickersonAuthor Commented:
That works, and I understand everything except for this part...

If you need this as array at some point, use this -
Dim myArray As String() = myStrings.ToArray()

What do you mean if I need this as array?  Why would I need to do that?
0
Neil FlemingConsultant and developerCommented:
I'd be tempted to have a single string, comma-separated, to which you add items as required.

dim MyString as string
if instr(Mystring,pair.primary)=0 then myString=Mystring & pair.primary  & ","

Open in new window


you can then split the string into an array, and assign the result directly to the .List property of a listbox.
dim aString() as string
'ditch the final comma
MyString=Strings.Left(MyString,len(MyString)-1)
aString=Split(MyString,",")
ListBox1.List=aString

Open in new window

0
Tom_HickersonAuthor Commented:
@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.
0
Neil FlemingConsultant and developerCommented:
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.
0
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.NET

From novice to tech pro — start learning today.