O'Reilly logo

VB.NET Language in a Nutshell, Second Edition by Steven Roman PhD, Paul Lomax, Ron Petrusha

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

Name

Hashtable.CopyTo Method

Class

System.Collections.Hashtable

Syntax

                  hashtablevariable.CopyTo(array, arrayindex)
array (required; Array of DictionaryEntry structures)

The destination of the items copied from the hash table

arrayindex (required; Integer)

The first index that is to receive an element of the hash table

Return Value

None

Description

Copies the hash table values into an array of DictionaryEntry structures. A DictionaryEntry structure is a key/value pair. Note that the array must be sized to accommodate the elements of the hash table prior to calling the CopyTo method.

Rules at a Glance

  • array must be a one-dimensional array.

  • Elements are copied from the hash table to array in the same order in which the hash table is iterated.

  • The CopyTo method copies each key/value pair in the hash table to a DictionaryEntry structure.

  • array, the array of DictionaryEntry structures, must be sized before calling the CopyTo method. This is illustrated in the example.

Example

Dim hshStates As New Hashtable
Dim aDE(  ) As DictionaryEntry
Dim oDE As DictionaryEntry

hshStates.Add("NY", "New York")
hshStates.Add("MI", "Michigan")
hshStates.Add("CA", "California")
hshStates.Add("WI", "Wisconsin")
hshStates.Add("VT", "Vermont")
hshStates.Item("WA") = "Washington"

Redim aDE(hshStates.Count - 1)
hshStates.CopyTo(aDE, 0)
For each oDE in aDE
   Console.WriteLine(oDE.Key & ": " & oDE.Value)
Next

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