Array of strings - add and remove

Drstein99

Junior Contributor
Joined
Sep 26, 2003
Messages
283
Location
Audubon, Nj
I want to declare an array of strings, I dont know how may will ever be, as little as 1 but may be over 50, throughout the execution of the program.

Dim sBackPage() As String

Now I want to ADD some strings to the list, and remove some strings from the list - how do I do that? :confused:
 
you could try an arraylist , they are pretty cool and can be added to / removed from the same as a listbox etc... eg:
Visual Basic:
        Dim arrlist As New ArrayList()
        Dim strStrings() As String = {"item1", "item2", "item3"}
        arrlist.AddRange(strStrings)

        arrlist.Remove("item2")

        MessageBox.Show(arrlist.Count)
 
I actually found a "collection" object, does exactly what I need:

Dim sBackPage As New Collection

sBackPage.Add("One") 'adds a page
sBackPage.Add("Two") 'another
sBackPage.Add("Three") ' and another

sBackPage.Remove(sBackPage.count) ' removes the last page


It works good, you think I have any problems with memory usage? I'm not too keen on working with objects TOO much, but what I do know is if I don't dispose or release certain things it makes a dirty mess.
 
if you are working in vb.net then everything is an object, you need not dispose your objects - that's what the GarbageCollector does for you - kills objects in memory when they are no longer needed, unlike vb6.
 
watch with collection

Hey there,

I used a StringCollection CLass for teh same purpose - which is similiar or exactly what I think you might be using. Just a heads up with this class.

One, yes it gets picked up by the GC - but there are some erratic behaviors if you don't release resources using it when you are trying to read information into it (from a file per se).

Two: The collection object is great when the array is about 150 or so elements in length - after that, performance gets less efficient. Also, you can't perform array methods (like sort) on a collection - you need an array or an arraylist.

For that, try wrapping the collection into an arraylist using the ArrayList.Adapter method. For example.

dim alist as ArraList = ArrayList.Adapter(your_collection_var)

alist.Sort
\\ or any other method to call on it

alist.nothing \\ when finished with the wrapper.

okay - just a jeads up. Hope it was helpful!

inzo
 
I'm not working with that many members, so I hope i'm in the clear. Thanks for the stringcollection advice, i'll switch over to string collection instead of the collection which is probably a collection of objects.

As far as wrapping collections into an arraylist, well - that stuffs above my head and I don't understand any of it.
 
Back
Top