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

Fill an ArrayList from an Array

Problem

You need to copy items from an array into an ArrayList.

Solution

You can add an entire range of items into an ArrayList quickly using the AddRange method.

Discussion

The ArrayList.AddRange method accepts any object that implements the ICollection interface (including an array or another ArrayList), and copies all the items it contains into the ArrayList.

Dim Colors() As String = {"blue", "green", "pink", "yellow"}

' Copy the strings in the Colors array into the List ArrayList.
Dim List As New ArrayList()
List.AddRange(Colors)

' Display the contents of the ArrayList.
Dim Item As String
For Each Item In List
    Console.WriteLine(Item)
Next

Keep in mind that if you are using a reference type, when you copy it into the ...

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