O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required