Visual Basic 2005 with .NET 3.0 Programmer's Reference

Normally Hashtables and SortedLists are case-sensitive. The CollectionsUtil class provides two shared methods, CreateCaseInsensitiveHashtable and CreateCaseInsensitiveSortedList, that create Hashtables and SortedLists objects that are case-insensitive.

The following code creates a normal case-sensitive SortedList. It then adds two items with keys that differ only in their capitalization. This works because a case-sensitive SortedList treats the two keys as different values. The code then creates a case-insensitive SortedList. When it tries to add the same two items, the list raises an exception, complaining that it already has an object with key value Sport.

Dim sorted_list As SortedList ' Use a normal, case-sensitive SortedList. sorted_list = New SortedList sorted_list.Add("Sport", "Volleyball") sorted_list.Add("sport", "Golf") ' Okay because Sport <> sport. ' Use a case-insensitive SortedList. sorted_list = CollectionsUtil.CreateCaseInsensitiveSortedList() sorted_list.Add("Sport", "Volleyball") sorted_list.Add("sport", "Golf") ' Error because Sport = sport.

If you can use case-insensitive Hashtables and SortedLists, you should generally do so. This prevents the program from adding two entries that are supposed to be the same but have different capitalization. For example, if one routine spells a key value “Law Suit” and another spells it “law suit,” the case-insensitive Hashtable or SortedList will quickly catch the error.

Категории