HashTable -- ContainsKey returns False, but can't add item

  • Thread starter Thread starter Daniel Wilson
  • Start date Start date
D

Daniel Wilson

LaborPool *Simulator::Controller::GetLaborPool(int LaborPoolID){

if (LaborPools->ContainsKey(__box(LaborPoolID))){

return dynamic_cast<LaborPool *>(LaborPools->Item[__box(LaborPoolID)]);

} else {

try {

LaborPool *newLP = new LaborPool(UltimateParent, DbConn, LaborPoolID);

LaborPools->Add(__box(newLP->ID), newLP);

return newLP;

} catch (Exception *Ex) {

throw Ex;

}

}

}



When LaborPoolID == 70,

LaborPools->ContainsKey(__box(LaborPoolID)) evaluates to false;

But LaborPools->Add(__box(newLP->ID), newLP) throws an exception,

"Item has already been added. Key in dictionary: "70" Key being added:

"70"".

What are we doing wrong?

Thanks.

dwilson
 
On Wed, 31 Aug 2005 09:54:27 -0400, "Daniel Wilson"

A couple of observations (warning: with minimal knowledge of .NET):

1) If your application is multi-threaded or re-entrant, could another
thread (or called function) have added the conflicting key?

2) While perhaps just a posting problem, you've been inconsistent in
referring to a key of 70 and a key of "70"; if ints and strings do not
hash consistently, your precheck is not efficacious.
 
Thanks for those observations.

re:#1, while the system technically is multi-threaded, it is a matter of a
parent thread for control & a worker thread accessing all the major data
structures. I don't believe threading is messing us up here.

re:#2, we were both checking and attempting to add an int (we also tried
Int32). The error message provided quotes around the thing, but we were not
passing a string.

Any other ideas about the problem?

Thanks.

dwilson
 
It turns out that code within the constructor was adding to the HashTable.

Thanks for looking at the problem anyway.

dwilson
LaborPool *Simulator::Controller::GetLaborPool(int LaborPoolID){

if (LaborPools->ContainsKey(__box(LaborPoolID))){

return dynamic_cast<LaborPool *>(LaborPools->Item[__box(LaborPoolID)]);

} else {

try {

LaborPool *newLP = new LaborPool(UltimateParent, DbConn, LaborPoolID);

LaborPools->Add(__box(newLP->ID), newLP);

return newLP;

} catch (Exception *Ex) {

throw Ex;

}

}

}



When LaborPoolID == 70,

LaborPools->ContainsKey(__box(LaborPoolID)) evaluates to false;

But LaborPools->Add(__box(newLP->ID), newLP) throws an exception,

"Item has already been added. Key in dictionary: "70" Key being added:

"70"".

What are we doing wrong?

Thanks.

dwilson
 
Back
Top