landingnormand
Member
- Joined
- Mar 26, 2009
- Messages
- 15
- Programming Experience
- Beginner
Hi,
When using the Dictionary class when the key and value types are your own custom types, is it possible to specify the property of the key type object to use with the ContainsKey method? Best described with an example:
I could just omit the lines with ContainsKey but I'd prefer to know how it's making the comparison and how to change it if required. I don't want to use an integer containing the ID for the key type because I want to reference the whole object later on again.
Thanks,
Norman
When using the Dictionary class when the key and value types are your own custom types, is it possible to specify the property of the key type object to use with the ContainsKey method? Best described with an example:
VB.NET:
Dim dict as New Dictionary(Of CutsomType, AnotheCustomType)
Dim obj1 as CustomType ' Custom type with 3 properties ID, LastName and FirstName
Dim obj2 as AnotherCustomType
obj1 = New Customtype(1, "Smith", "John")
obj2 = New AnotherCustomType("a", "b")
If not dict.ContainsKey(obj1) Then
dict.Add(obj1, obj2)
End If
' obj2 with key obj1 added successfully as no object of type CustomType with ID equal to 1 exists
obj1 = New Customtype(2, "Jones", "Alan")
obj2 = New AnotherCustomType("d", "e")
If not dict.ContainsKey(obj1) Then
dict.Add(obj1, obj2)
End If
' obj2 with key obj1 added successfully as no object of type CustomType with ID equal to 1 exists
obj1 = New Customtype(1, "Bloggs", "Joe")
obj2 = New AnotherCustomType("f", "g")
If not dict.ContainsKey(obj1) Then
dict.Add(obj1, obj2)
End If
' obj2 with key obj1 NOT added as an object of type CustomType with ID equal to 1 exists
I could just omit the lines with ContainsKey but I'd prefer to know how it's making the comparison and how to change it if required. I don't want to use an integer containing the ID for the key type because I want to reference the whole object later on again.
Thanks,
Norman