Which is best Arraylist or Collection?

p_nivalis

Active member
Joined
Oct 1, 2004
Messages
33
Programming Experience
5-10
As i've mentioned in a cpl other threads, I'm learning the vb.net robes form being a vb6 dev.

As a result, I am new to arraylists. From what I gather they are the same as collections? Can someone explain the difference between them, and which is generally best to use?

Thanks!
 
The Collection is part of the VB.NET Runtime and exists solely for compatibility with VB6 code. You should NEVER write new code using a Collection object. Unless you have VB6 code upgraded to VB.NET that already uses Collection objects you should not even consider them as an option. If you're writing only new VB.NET code then forget the Collection class even exists.
 
Here it is important to distinct the Collection class jmcilhinney just eradicated and .Net collections from the System.Collections namespace. The latter is where the ArrayList belongs, and it is basically a collection of type Object. The main interfaces it implements (IList and ICollection) is the same as the former. In .Net 1.1 you would in need of a strong collection class inherit from the System.Collections.CollectionBase class. In .Net 2.0 generics have come to life where you easily can create strongly typed collections of various types (Of T), for example:
Dim myColorList As New List(Of Color)
 
Thank you. I wasn't aware that collections were solely a VB6 leftover in .NET. I see no problem with using arraylists instead, since it acts apparently almost identical anyway. :)

Just a couple further questions. In collections you could also add a Key to each item, so you could search for or use the key as well. I used to use this almost like the Tag property on alot of controls, to store some extra info. For example if I were to write a IM client, I would store the users ID as the item, and then the users Name as the Key or Tag. Then if someone wanted to change the name of the user associated with that ID, I could easily just alter the Key/Tag to match it.

Is this still possible w/arraylists? Right off the bat I didnt see it there.

Second question, when dimming a List object, is it simply just another way of saying arraylist but defined to only allow one type of object in it?

Thanks for all the help.
 
~collections~ as a term and implementation isn't a classic VB left over, the Collection class from the Microsoft.VisualBasic namespace is.

The System.Collections.HashTable class is a value/key pair collection from the .Net framework. It also only takes about three lines of code to write your own search method for any collection.
 
As has been said, "collection" is a general term, whereas I was referring specifically to the Collection class, which I'm sure was the actual question. Having said that, VB.NET provides numerous types of collections. The ArrayList is just the most commonly used in .NET 1.x. It is basically an dynamic array of Objects. VB.NET also provides the Hashtable and SortedList if you need a keyed collection, and various other more specialised types too. There is also CollectionBase and DictionaryBase if you need to create your own strongly-typed collection.

.NET 2.0 adds generic collections, which address the issue of item typing with classes like the ArrayList. The ArrayList will not prevent adding of any type of object at all, so if you want strong-typing in .NET 1.x you have to define your own class that inherits CollectionBase. In .NET 2.0 you simply create a generic List and specify the type of the items. No additional code required.
 
Back
Top