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?
 
Jayadev NairConnect With a Mentor Application 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
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Tom_HickersonConnect With a Mentor Author 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 FlemingConnect With a Mentor Independent consultantCommented:
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 FlemingIndependent consultantCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.