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

Get VB.NET Language in a Nutshell, Second Edition 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.