Using HashTables

Team Fly 

Page 37

example with a ListBox and Button, replace the Button's Click event with this code to see how to bind an ArrayList to a ListBox:

 Private Sub Button1_Click(ByVal sender As System.Object, _  ByVal e As System.EventArgs) Handles Button1.Click        Dim Monkey As New ArrayList()        Monkey.Add(''A")        Monkey.Add("B")        Monkey.Add("C")        Monkey.Add("D")        Monkey.Add("E")        Monkey.Add("F")        ListBox1.DataSource = Monkey    End Sub 

Enumerators

You're used to looping with For...Next, While, and other structures, but now Microsoft encourages us to use enumerators when looping through a collection class. This example illustrates how to rewrite the previous example to display both elements in the RangeOfArrList ArrayList:

 Dim RangeArrListEnumerator As System.Collections.IEnumerator = _                                    RangeOfArrList.GetEnumerator()         While RangeArrListEnumerator.MoveNext()             Console.Write(RangeArrListEnumerator.Current)             Console.WriteLine()         End While 

Using HashTables

The collection class called a HashTable is quite similar to the ArrayList in both design and features. However, a HashTable permits "strong data typing": You can give each element a name in addition to its index number.

In some situations, it's easier to work with a collection if each element is labeled. Say your collection holds the foods eaten by each animal in your private zoo. It's simpler to manage the data if each element is named after a different animal:

 Dim Food As New Hashtable()      Food.Add("Lion", "Meat")      Food.Add("Bear", "Meat")      Food.Add("Penguin", "Fish")      Console.WriteLine(Food.Item("Bear")) 
Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net