need to create a dictionary where keys are list(Of String) or array of string
i found this link C# List as Dictionary key
but i need an help to understand how to do it with list(of String)
Class test
Sub TryTest()
myDict.Add(New List(Of String) From {"Huey", "Dewey"}, actions.Sleeping)
myDict.Add(New List(Of String) From {"Dewey", "Louie"}, actions.Playing)
Dim newList As New List(Of String) From {"Dewey", "Louie"}
If myDict.ContainsKey(newList) Then
MsgBox("myDict contains the list of String, as Key Value")
Else
MsgBox("myDict don't contains the list of String, as Key Value")
End If
End Sub
Dim myDict As New Dictionary(Of List(Of String), actions)
End Class
Enum actions
Sleeping
Eating
Studying
Playing
End Enum
I expected that the dictionary output that contains the key.
P.S. Since c# it's close to vb.net, and on the net there are lot's of c#/vb.net translators that translate easily, please, also c# help are appreciated.
UPDATE (after Jeppe Stig Nielsen helps, i tried to implement a class that inherits EqualityComparer, but it doesn't work... maybe i mistake something in syntax... do someone know what's wrong in my approach?
Class test
Sub TryTest()
myDict.Add(New List(Of String) From {"Huey", "Dewey"}, actions.Sleeping)
myDict.Add(New List(Of String) From {"Dewey", "Louie"}, actions.Playing)
Dim newList As New List(Of String) From {"Dewey", "Louie"}
If myDict.ContainsKey(newList) Then
MsgBox("myDict contains the list of String, as Key Value")
Else
MsgBox("myDict don't contains the list of String, as Key Value")
End If
Try
myDict.Add(newList, actions.Eating)
Catch ex As Exception
MsgBox("myDict don't allow me to add an already present List (Of String), as Key Value")
End Try
End Sub
Dim myDict As New Dictionary(Of List(Of String), actions)(New ListComparer)
End Class
Enum actions
Sleeping
Eating
Studying
Playing
End Enum
NotInheritable Class ListComparer
Inherits EqualityComparer(Of List(Of String))
Public Overrides Function Equals(ByVal x As List(Of String), ByVal y As List(Of String)) As Boolean
Return StructuralComparisons.StructuralEqualityComparer.Equals(x, y)
End Function
Public Overrides Function GetHashCode(ByVal x As List(Of String)) As Integer
Return StructuralComparisons.StructuralEqualityComparer.GetHashCode(x)
End Function
End Class