The ability to use collections is important in any language and practical for any application. Collections allow you to manage groups of objects. In this article, I'll look at how to use an ArrayList class, which provides basic functionality that is useful to most applications. I'll also cover the basics about the methods within the class.
An ArrayList class
An ArrayList class represents a dynamically sized, index-based collection of objects. In the cases where you don't know the number of objects that you would store, an ArrayList class is a good option to utilize.
The System.Collections.ArrayList class provides general collection functionality that is suitable for many uses. The class allows you to dynamically add and remove items from a list. Items are accessed by using an index of the item.
The following example shows how to create a new instance of an ArrayListclass:
Dim arrayListInfo As New System.Collections.ArrayList()
The Add method
Once instantiated, you can add items to an ArrayList class using the Add method. For example, the following code shows how to add items to an existing ArrayList class:
Dim arrayListInfo As New ArrayList()
ArrayListInfo.Add("Item1")
arrayListInfo.Add("Item2")
arrayListInfo.Add("Item3")
An ArrayList class is a zero-based collection; therefore, the first object added to the list is assigned the index zero, and all subsequent objects are assigned to the next available index.
The Item property indexer
The Item property of an ArrayList class gets or sets the element at the specified index. You can retrieve an item from an ArrayList class at a particular index by using the Item property.
This is the default property for collections in VB.NET. This means you can access it without using the Item property name, allowing syntax similar to that of an array.
The following example shows how to retrieve a reference to an object in a collection:
Dim objItem As Object
objItem = arrayListInfo(0)
or
Dim objItem As Object
objItem = arrayListInfo.Item(0)
The Remove and RemoveAt methods
In order to remove an item from a collection, you can utilize either the Remove or RemoveAt method. The Remove method requires a reference to an object contained within the collection as a parameter and removes that object from the collection. The RemoveAtmethod allows you to remove an object at a particular index.
As items are removed from a collection, the index numbers are reassigned to occupy any available spaces; so, index values are not static and might not always return the same reference.
The Count property
The Count property returns the current number of items in a collection. The collections are always zero-based; therefore, the Count property will always return a value that is one greater than the upper bound of the array.
Miss a tip?
Check out the Visual Basic archive, and catch up on the most recent editions of Irina Medvinskaya's column.
Advance your scripting skills to the next level with TechRepublic's free Visual Basic newsletter, delivered each Friday. Automatically sign up today!



