Use a Dynamic ArrayList

Problem

You want to use a one-dimensional array that allows items to be inserted and removed dynamically and resizes itself automatically.

Solution

Use the System.Collections.ArrayList class.

Discussion

The ArrayList class is quite straightforward. Items are added using the Add method. Items can be removed using the Remove, RemoveRange, or RemoveAt methods. The latter two methods remove items based on their index numbers, while Remove searches the collection for the indicated item and then removes it. Instead of using the GetLength method, as you would with an array, you use the equivalent property Count.

Dim List As New ArrayList() List.Add("blue") List.Add("green") List.Add("yellow") List.Add("red") ' Remove "blue" by index ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.