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

Class

System.Collections.Hashtable

Syntax

                  hashtablevariable.Keys(  )
hashtablevariable (required; Hashtable object)

A reference to a Hashtable object

Return Value

An ICollection interface containing the keys in the hash table

Description

Returns an ICollection interface that contains the keys in the hash table. There is not much we can do with an ICollection object except copy it to an array of Objects using its CopyTo method, as the following example illustrates.

Example

Dim hshStates As New Hashtable
Dim iColl As ICollection
Dim aKeys(  ), sKey As String

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"
hshStates.Item("AK") = "Alaska"

Redim aKeys(hshStates.Count - 1)
iColl = hshStates.Keys
iColl.CopyTo(aKeys, 0)
for each sKey in aKeys
   Console.WriteLine(hshStates.Item(sKey))
Next

Programming Tips and Gotchas

You can work around the inconvenience of calling the ICollection object’s CopyTo method to convert the interface to another object by defining a class that inherits from or implements ICollection.

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