Search code examples
vb.netdictionarycase-insensitive

VB.NET: Inheriting from case-insensitive dictionary type


I'm using Visual Studio 2010. I want to create a simple class which inherits from a case-insensitive dictionary type. I can inherit from a case-sensitive dictionary type as follows:

Public Class clsMyDictionary
  Inherits Dictionary(Of String, String)
End Class

A case-insensitive dictionary object is normally created as follows:

Dim d = New Dictionary(Of String, String)(StringComparer.OrdinalIgnoreCase)

So I would like to do something like the following, so my dictionary class functions as case-insensitive, but it results in a syntax error:

Public Class clsMyDictionary
  Inherits Dictionary(Of String, String)(StringComparer.OrdinalIgnoreCase)
End Class

Specifying StringComparer isn't allowed in that context. Is there another way of doing it?


Solution

  • Just call the base constructor with the OrdinalIgnoreCase parameter.

    Public Class clsMyDictionary
      Inherits Dictionary(Of String, String)
    
      public sub new()
         MyBase.New(StringComparer.OrdinalIgnoreCase)
      end sub
    
    End Class