In Visual Basic .NET, the ArrayList class is used to create an array (or list) of objects. Unlike simple arrays, ArrayLists are designed to have no fixed size – contents can be added or removed while the program is running. Although arrays can be re-dimensioned in VB.NET, they are not built to be used this way. In situations where this is likely to occur often, performance may suffer and it is often preferable to use an ArrayList.
To make full use of the information and examples presented here, a working knowledge of programming in Visual Basic.NET is recommended. Beginners can learn application programming with VB.NET and Microsoft Visual Studio 2013 at Udemy.com. It is also possible to work with ArrayList objects in Visual Basic for Applications (VBA), and a very short example of this is shown below. Readers who are new to the concepts of working with VBA in Excel, or unaware of the differences between VBA and VB.NET might care to take a useful course in writing Excel macros using Visual Basic.
Declaring ArrayLists in VB.NET
ArrayLists are part of the System.Collections namespace that is usually imported automatically when a project is created in Microsoft Visual Studio. If your project does not import this namespace, or if you wish to check, you can find the imported namespaces in the References page of the Project Designer.
To open the References page in Microsoft Visual Studio:
- In the Solution Explorer, right-click the project name and then click Properties.
- In the Project Designer, click the References tab.
System.Collections is part of the Visual Basic core library and will already be in the references. The list of imported namespaces is at the bottom of the References tab. The checkbox next to System.Collections should be checked so that the contents of the namespace are available for use by your program.
In a Visual Basic code module, ArrayLists are declared using the Dim statement:
Dim myList As New ArrayList
Declaring ArrayLists in VBA
In Visual Basic for Applications, ArrayList is not a built-in object type. However, VBA can create objects from the .NET framework and work with them directly.
To create an ArrayList from a VBA function or macro:
- In the Visual Basic Editor, in the desired module, declare a variable as an object type:
Dim myList As Object
- Set the variable to an instance of ArrayList using the CreateObject() function:
Set myList = CreateObject("System.Collections.ArrayList")
- Work with the ArrayList object as described in the following sections.
Working with ArrayLists
Adding items to an instance of an ArrayList is a relatively straightforward process. There are four methods for doing this: Add(), AddRange(), Insert(), and InsertRange().
Add(value As Object) accepts a single argument, which can be any object type. The item is then added to the end of the list.
myList.Add("Add a string to the list.")
You can use AddRange(c As ICollection) to append the contents of one list to another. AddRange() accepts a single argument, which must be an object that is derived from ICollection. The ArrayList class is derived from ICollection and so you can add one list to another using a call such as:
Dim myList As New ArrayList Dim secondList As New ArrayList … myList.Add(secondList)
Once the list contains some items, they can be accessed using the same array-addressing syntax that is used when working with simple arrays. For example, to refer to the first item in the list:
Insert(index As Integer, value As Object) and InsertRange(index As Integer, c As ICollection) are very similar to the add functions described above, except that they accept two arguments. The first argument is a number that specifies the index at which the second argument should be inserted into the list.
To remove items from an ArrayList, you can use either the method Remove(value As Object) or RemoveAt(index As Integer). Remove() accepts a single object and, if that object is found in the list, it is removed. The method RemoveAt(index As Integer) accepts only a single number – the item at the position specified by this number is deleted from the list.
To clear a list entirely, use the method Clear(). This accepts no parameters and simply deletes everything that is currently in the list:
Note: In VBA, empty calls to subroutines like Clear() are made without parenthesis.
Searching through ArrayLists
If you want to check whether an ArrayList already contains an item, you can use the method Contains(value As Object). This returns true if the object or value is found the in the list, or false if it cannot be found.
When checking for simple types, such as strings or numbers, the Contains() method will look for any item with a value that matches the one specified in the call. For example:
If (myList.Contains("A string!")) Then System.Diagnostics.Debug.WriteLine("Found it!") End If
Contains() makes decisions based on calls to the items’ Equals() and CompareTo() methods. For complicated objects, it may be necessary to loop through the ArrayList and implement your own logic for finding items.
The two most common loops used for looping through lists are for…next and for each…next.
A standard Visual Basic for…next loop can be constructed using the ArrayList’s method Count() to return the number of items in the list. Items are then retrieved from the list in the body of the loop.
With a for each…next loop, each item in the ArrayList is processed in turn and is assigned to the declared variable.
Items in an ArrayList are stored in the order that they are added to the list, except when Insert() or InsertRange() are used to insert items into a specific position. The order of items can be reversed with a call to:
The ArrayList class also has a built-in method for sorting simple object types – appropriately named Sort() – using the QuickSort algorithm.
The basic form of this method accepts no arguments and returns no values, it just sorts the list in alphabetical, ascending numerical, or chronological order depending on whether the items are based on strings, numbers, or dates.
Dim myList As New ArrayList myList.Add("Elephant") myList.Add("Cat") myList.Add("Anteater") myList.Add("Deer") myList.Add("Bear") myList.Sort() For Each item In myList System.Diagnostics.Debug.WriteLine(item) Next
An override for Sort() accepts objects that implement IComparer, to provide sorting based on arbitrary comparisons.
Programmers who are used to simple arrays might find the leap to using collection objects to be quite large. As object types, many of the object-oriented programming techniques used for other objects will also work on ArrayLists. For examples and explanations of object-oriented programming in Visual Basic .NET, see Learn VB.NET with Microsoft Visual Studio 2013.