Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I don't think you understand how dictionaries work. The values of a dictionary are accessed by their unique keys. Internally they are looked up using the hash codes produced by the keys. To work properly the hash codes require a few properties:</p> <ul> <li><p>Two objects that compare as equal must have identical hash codes.</p></li> <li><p>If a mutable object produces a hash code that relies on its mutable state it should not be used as a key in a dictionary. The reason is simple: If you put such an object into a hash table and mutate it you will not be able to find it again.</p></li> </ul> <p>The hash codes are produced by the <a href="http://msdn.microsoft.com/library/system.object.gethashcode.aspx" rel="nofollow"><code>GetHashCode</code></a> function that might be overriden by any type. You use <code>List(Of Object)</code> as key values. <code>List(Of Object)</code> does not override <code>GetHashCode</code>. It uses the default implementation of the Object type. Although this implementation meets the stated requirements it is definitely not what you are looking for.</p> <p>I noticed that the lists you try to use as keys always have the same structure: an integer, a string and another integer. <code>List(Of Object)</code> is not a good choice for keys. But maybe you can create a type that can be used as a key:</p> <pre><code>Public Class MyKey Private ReadOnly _firstValue As Integer Private ReadOnly _secondValue As String Private ReadOnly _thirdValue As Integer Public Sub New(firstValue As Integer, secondValue As String, thirdValue As Integer) _firstValue = firstValue _secondValue = secondValue _thirdValue = thirdValue End Sub Public ReadOnly Property FirstValue As Integer Get Return _firstValue End Get End Property Public ReadOnly Property SecondValue As String Get Return _secondValue End Get End Property Public ReadOnly Property ThirdValue As Integer Get Return _thirdValue End Get End Property Public Overloads Function GetHashCode() As Integer Dim hashCode As Integer = 31 hashCode = hashCode + 17 * _firstValue hashCode = hashCode + 17 * _secondValue.GetHashCode() hashCode = hashCode + 17 * _thirdValue Return hashCode End Function Public Overloads Function Equals(obj As Object) As Boolean If TypeOf obj Is MyKey Then Dim other As MyKey = CType(obj, MyKey) Return _firstValue = other._firstValue And _secondValue = other._secondValue And _thirdValue = other._thirdValue Else Return False End If End Function End Class </code></pre> <p>This type can be used for dictionary keys. It produces a proper hash code and overrides <a href="http://msdn.microsoft.com/library/bsc2ak47.aspx" rel="nofollow"><code>Equals</code></a> to compare the contents instead of the references. Also it is immutable to ensure that the hash code never changes.</p> <p>Dictionaries require unique keys. Since your keys aren't unique the values need to be of type List(Of Integer). Adding a value requires some extra work. First you have to check if the key already exists in the dictionary. If not create a new entry:</p> <pre><code>Dim dictionary As New Dictionary(Of MyKey, List(Of Integer)) Add(dictionary, New MyKey(12, "SomeString", 3), 54) Add(dictionary, New MyKey(8, "SomeAnotherString", 3), 6) Add(dictionary, New MyKey(12, "SomeString", 3), 15) Public Sub Add(dictionary As Dictionary(Of MyKey, List(Of Integer)), key As MyKey, value As Integer) Dim list As List(Of Integer) = Nothing If Not dictionary.TryGetValue(key, list) Then list = New List(Of Integer)() dictionary.Add(key, list) End If list.Add(value) End Sub </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload