Hashtable key not recognised in collection

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vatechsKirk
    New Member
    • Jan 2010
    • 2

    Hashtable key not recognised in collection

    I am using a hashtable to cache incoming UDP messages before my application processes them. After adding pairs to the hashtable, I am experiencing strange behaviour when interogating the collection, such that a containsKey() enquiry returns false, even though the key exists and is visible in the locals window. Spelling etc is correct, manually calling GetHashCode on the key (String) at each point (add() and containsKey()) is returning different values for the same key. To complicate things more, some keys are matched successfully.

    Any suggestions welcome, code snippets to follow.
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    I might suggest using the Queue class instead of a hashtable.
    It just seems like it meant for this type of need.
    Originally posted by MSDN
    Queues are useful for storing messages in the order they were received for sequential processing. Objects stored in aQueue(T) are inserted at one end and removed from the other.

    The capacity of a Queue(T) is the number of elements the Queue(T) can hold. As elements are added to a Queue(T), the capacity is automatically increased as required by reallocating the internal array. The capacity can be decreased by calling TrimExcess.

    Queue(T) accepts a null reference (Nothing in Visual Basic) as a valid value for reference types and allows duplicate elements.

    Comment

    • vatechsKirk
      New Member
      • Jan 2010
      • 2

      #3
      Hi tlhintoq, thanks for your reply, but a queue would not be suitable as the udp data I'm saving has an identifier (key), by which I need to reference its payload. Also, incoming data may not be processed in order. So I need to get the hashtable working correctly.

      Comment

      Working...