COLLECTIONSUTIL

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.

Example program UseCaseInsensitiveSortedList, which is available for download on the book’s website, uses code similar to the following to create 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 may want to do so to prevent 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 ...

Get Visual Basic 2012 Programmer's Reference 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.