OrderedDictionary

VB.NET:
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] orddic [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Specialized.OrderedDictionary([/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] CaseInsensitiveComparer)
[/SIZE]

 
CaseInsensitiveComparer

On this declaration I got the following exception:

Unable to cast object of type 'System.Collections.CaseInsensitiveComparer' to type 'System.Collections.IEqualityComparer'.

Thanks,
Greg
 
Didn't try to compile to code above, se solution below.
 
Last edited:
You have to create a new comparer class that implements IEqualityComparer where you use the CaseInsensitiveComparer for comparisons, so:
VB.NET:
Public Class CIComparer
Implements IEqualityComparer
 
Dim myComparer As CaseInsensitiveComparer
 
Public Sub New()
myComparer = CaseInsensitiveComparer.DefaultInvariant
End Sub
 
Public Function Equals1(ByVal x As Object, ByVal y As Object) _
As Boolean Implements IEqualityComparer.Equals
If (myComparer.Compare(x, y) = 0) Then
Return True
Else
Return False
End If
End Function
 
Public Function GetHashCode1(ByVal obj As Object) _
As Integer Implements IEqualityComparer.GetHashCode
Return obj.ToString().ToLower().GetHashCode()
End Function
 
End Class
Then you put an instance of this class into the constructor of OrderedDictionary:
VB.NET:
Dim orddic As New Specialized.OrderedDictionary(New CIComparer)
 
Back
Top