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 case-insensitive Hashtable or SortedList will quickly catch the error. Neither will notice an error if part of your program spells this “LawSuit.” (You could also add extra logic to remove spaces and special symbols to increase the chances of finding similar terms that should be the same, but a discussion of these sorts of methods is beyond the scope of this book.)

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.58.168.80