Using CaseInsensitiveComparer in Visual Basic

Comparing objects has always been a tricky affair. Luckily, with each new version of the .NET Framework Microsoft makes things a little bit easier. With the advent of the .NET Framework, many moons ago, a lot of new namespaces were created and a lot more effort was put into making comparing the equality of two or more objects easier.

Strings

Before I get too technical, let me get back to the basics and explain Strings more properly:

The term ‘Strings’ always seem to amount to some sort of headache among new programmers. We all know what a string is: It is any set of alphanumeric input. The trick comes in knowing how to manipulate strings properly. Once you have learned this skill, you’re halfway to becoming a great programmer!

What String Functions Are There?

The key to working with strings is to know what functions are available. There are actually many string functions available to Visual Basic developers. A more detailed list of all of the string functions can be found at this link.

CaseInsensitiveComparer

The CaseInsensitiveComparer class does case-insensitive comparison on string objects. CaseInsensitiveComparer implements the IComparer interface that supports case-insensitive comparisons on strings. For more information on CaseInsensitiveComparer, have a look here.

String comparisons might have different results, depending on the culture.

Globalization

Globalization is basically the process of making your application culture-friendly. You have to realize that the users of your application may speak a different language than you, and therefore their language adheres to different rules, apart from being a different language. For example, some languages are displayed left to right, while others should be displayed right to left. Now, you have to not only think about the different language, but different currencies, date, and time settings as well as different colors being used with different cultures.

With Globalization, you can make your application useable with any culture irrespective of its language.

Here are some more articles on Globalization:

Localization

Localization allows you to customize your application according to a certain culture.

Here is more information on Localization.

IComparer Interface

The IComparer Interface simply exposes a method that compares two objects. For more information regarding the IComparer Interface, have a look here.

Our Project

Start Visual Studio and create a new Visual Basic Console Application. Add the following into your Module:

Imports System.Globalization
Imports System.Collections
Imports System.Collections.Specialized

Class Program
   Shared Sub Main(ByVal args() As String)

   ' Make the dictionary case insensitive
   Dim list as New ListDictionary( _
      New CaseInsensitiveComparer(CultureInfo.InvariantCulture))

   ' Add some items
      list("De Verenigde Staten van Amerika") = _
         "United States of America"
      list("Canada") = "Kanada"
      list("España") = "Spanje"

   ' Show the results
   Console.WriteLine(list("españa"))
      Console.WriteLine(list("CANADA"))

   Console.Read()

   End Sub

End Class

This code basically translates various country names to Dutch and then does a case-insensitive comparison between the given strings.

Conclusion

There is always much more than meets the eye using any technology, especially with string comparisons.

Hannes DuPreez
Hannes DuPreez
Ockert J. du Preez is a passionate coder and always willing to learn. He has written hundreds of developer articles over the years detailing his programming quests and adventures. He has written the following books: Visual Studio 2019 In-Depth (BpB Publications) JavaScript for Gurus (BpB Publications) He was the Technical Editor for Professional C++, 5th Edition (Wiley) He was a Microsoft Most Valuable Professional for .NET (2008–2017).

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read