J
Jeff Mason
I'm trying to define a collection which inherits from DictionaryBase. This
collection requires a composite key (comprised of two string values) and the value
associated with the key is an integer.
I thought this would be easy.
I defined a "key" class (called 'AccessKey') which exposed two string properties and
implemented a GetHashCode which simply returned the GetHashCode of their
concatenation.
I defined an add method in my derived collection class as:
Public Sub Add(ByVal Key As AccessKey, ByVal Value As Integer)
MyBase.Dictionary.Add(Key, Value)
End Sub
This appeared to work fine. When I ran code such as:
myCollection.Add(new AccessKey("string1","string2"), 1)
myCollection.Add(new AccessKey("string3","string4"), 99)
in debug mode it appeared that the collection contained the right values with my
AccessClass as a key.
But try as I might, I couldn't get the thing to *return* the correct value for a
given key. I implemented an Item property as:
Default ReadOnly Property Item(ByVal key As AccessKey) As Integer
Get
Return CType(Dictionary.Item(key), Integer)
End Get
End Property
but this always returns a 0 no matter what key value I use. Thus:
dim somevalue as integer = myCollection(new AccessKey("string3","string4"))
returns a 0 instead of 99.
I'm doing something stupid, but what?
I can get around this by throwing out the concept of a key class and just
concatenating the two strings together and using the resultant string as a key. I've
tried this and it works fine, but I really want to understand what I am missing when
I attempt to define and use a composite key class.
collection requires a composite key (comprised of two string values) and the value
associated with the key is an integer.
I thought this would be easy.
I defined a "key" class (called 'AccessKey') which exposed two string properties and
implemented a GetHashCode which simply returned the GetHashCode of their
concatenation.
I defined an add method in my derived collection class as:
Public Sub Add(ByVal Key As AccessKey, ByVal Value As Integer)
MyBase.Dictionary.Add(Key, Value)
End Sub
This appeared to work fine. When I ran code such as:
myCollection.Add(new AccessKey("string1","string2"), 1)
myCollection.Add(new AccessKey("string3","string4"), 99)
in debug mode it appeared that the collection contained the right values with my
AccessClass as a key.
But try as I might, I couldn't get the thing to *return* the correct value for a
given key. I implemented an Item property as:
Default ReadOnly Property Item(ByVal key As AccessKey) As Integer
Get
Return CType(Dictionary.Item(key), Integer)
End Get
End Property
but this always returns a 0 no matter what key value I use. Thus:
dim somevalue as integer = myCollection(new AccessKey("string3","string4"))
returns a 0 instead of 99.
I'm doing something stupid, but what?
I can get around this by throwing out the concept of a key class and just
concatenating the two strings together and using the resultant string as a key. I've
tried this and it works fine, but I really want to understand what I am missing when
I attempt to define and use a composite key class.