473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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

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

Nov 17 '05 #1
3 1562
On Wed, 31 Aug 2005 09:54:27 -0400, "Daniel Wilson"
<d.******@Embtrak.com> wrote:

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.

--
Sev
"And I am but a thought of mine, an egotisticality."
Nov 17 '05 #2
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
"Severian [MVP]" <se******@chlamydia-is-not-a-flower.com> wrote in message
news:48********************************@4ax.com...
On Wed, 31 Aug 2005 09:54:27 -0400, "Daniel Wilson"
<d.******@Embtrak.com> wrote:

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.

--
Sev
"And I am but a thought of mine, an egotisticality."

Nov 17 '05 #3
It turns out that code within the constructor was adding to the HashTable.

Thanks for looking at the problem anyway.

dwilson
"Daniel Wilson" <d.******@Embtrak.com> wrote in message news:eC**************@TK2MSFTNGP10.phx.gbl...
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

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: davidw | last post by:
I always use NameValueCollection. But I read an article says the only differece between Hashtable and NameValueCollection is that NameValueCollection could accept more than one value with same key?...
5
by: Matthias Kwiedor | last post by:
Hi! What i want to do, is to put an ArrayList into a Hashtable. So i can access the ArrayList fast over the key-indexing of the Hashtable. What i did is to put the ArrayList into the...
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
5
by: Victor Paraschiv | last post by:
I need to serialize into an XML file a hashtable. From MSDN at XmlSerializer:...
8
by: Robin Tucker | last post by:
When I create a hashtable hashing on Object-->Item, can I mix "string" and "integer" as the key types? I have a single thumbnail cache for a database with (hashed on key) and a file view (hashed...
5
by: Dick | last post by:
Hello, I'm trying to serialize a class with a Hashtable within: ' Class code: Imports System.Collections Class clsOptions Public countID As Integer Public persons As New Hashtable End Class
1
by: Ivan | last post by:
is it possible to make a universal hashtable that would be able to serialize itself to xml format? universal one, meaning I don't have to know about the type of keys and values in advance. Ivan
0
by: Matthew Wieder | last post by:
I have the following requirement: A form with 2 ListViews. lvSource which contains a list of items that the user can select from and lvTarget where the selected item(s) end up. Items should...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.