472,805 Members | 1,514 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Hashtable caveat

Hi All,

Be careful when indexing Hashtable elements by number. The following
code will create two quite distinct entries in an Hashtable:

myHashtable.Add((int)666, "Foo");
myHashtable.Add((long)666, "Foo");

Always ensure all indexing is done by the objects of the same type -
it's probably safest to explicitly cast them.

Yours,
Duncan Bayne
--
+-----------------------------------------------------------------+
| Duncan Bayne phone (+64) 027 2536395 email dh*****@ihug.co.nz |
| ============ icq# 115621676 msn dh*****@hotmail.com |
| |
| web http://homepages.ihug.co.nz/~dhbayne/ |
| http://groups.yahoo.com/group/akl_airsoft/ |
| http://groups.yahoo.com/group/wakeup_nz/ |
+-----------------------------------------------------------------+
| "The ultimate result of shielding men from the effects of folly |
| is to fill the world with fools." |
| |
| - Herbert Spencer. |
+-----------------------------------------------------------------+

Nov 15 '05 #1
2 1190
Duncan,

That's all because of the number 666 you've used for testing :-) Well, just
kidding. Hashtable uses a call to Equals to finally compare keys. While it
is obvious that there's an overload of Equals that accepts two integers or
two longs and compares them by value, Int32's Equals always returns false
when its argument is not of a type Int32.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"Duncan Bayne" <dh*****@ihug.co.nz> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
Hi All,

Be careful when indexing Hashtable elements by number. The following
code will create two quite distinct entries in an Hashtable:

myHashtable.Add((int)666, "Foo");
myHashtable.Add((long)666, "Foo");

Always ensure all indexing is done by the objects of the same type -
it's probably safest to explicitly cast them.

Yours,
Duncan Bayne
--
+-----------------------------------------------------------------+
| Duncan Bayne phone (+64) 027 2536395 email dh*****@ihug.co.nz |
| ============ icq# 115621676 msn dh*****@hotmail.com |
| |
| web http://homepages.ihug.co.nz/~dhbayne/ |
| http://groups.yahoo.com/group/akl_airsoft/ |
| http://groups.yahoo.com/group/wakeup_nz/ |
+-----------------------------------------------------------------+
| "The ultimate result of shielding men from the effects of folly |
| is to fill the world with fools." |
| |
| - Herbert Spencer. |
+-----------------------------------------------------------------+


Nov 15 '05 #2
Duncan,
Which is where in C# 2.0 (Whidbey) it will be "better" to use
System.Collections.Generic.Dictionary<K, V> as the key will be strongly
typed!

http://longhorn.msdn.microsoft.com/l...ictionary.aspx

Something like:

Dictionary<int, string> myHashTable = new Dictionary<int, string>();
myHashTable.Add(666, "Foo");
myHashTable.Add((long)666, "Foo"); // error! long does not fit in an
integer!

Also the above will be more efficient then HashTable as the key will not be
boxed!

Hope this helps
Jay

"Duncan Bayne" <dh*****@ihug.co.nz> wrote in message
news:uA**************@TK2MSFTNGP09.phx.gbl...
Hi All,

Be careful when indexing Hashtable elements by number. The following
code will create two quite distinct entries in an Hashtable:

myHashtable.Add((int)666, "Foo");
myHashtable.Add((long)666, "Foo");

Always ensure all indexing is done by the objects of the same type -
it's probably safest to explicitly cast them.

Yours,
Duncan Bayne
--
+-----------------------------------------------------------------+
| Duncan Bayne phone (+64) 027 2536395 email dh*****@ihug.co.nz |
| ============ icq# 115621676 msn dh*****@hotmail.com |
| |
| web http://homepages.ihug.co.nz/~dhbayne/ |
| http://groups.yahoo.com/group/akl_airsoft/ |
| http://groups.yahoo.com/group/wakeup_nz/ |
+-----------------------------------------------------------------+
| "The ultimate result of shielding men from the effects of folly |
| is to fill the world with fools." |
| |
| - Herbert Spencer. |
+-----------------------------------------------------------------+

Nov 15 '05 #3

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

Similar topics

5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
8
by: SenthilVel | last post by:
how to get the corresponding values for a given Key in hashtable ??
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...
16
by: Sreekanth | last post by:
Hello, Is there any better collection than HashTable in terms of performance, when the type of the key is integer? Regards, Sreekanth.
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...
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...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
2
by: archana | last post by:
Hi all, I am having one confusion regarding hashtable. I am having function in which i am passing hashtable as reference. In function i am creating one hashtable which is local to that...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.