471,092 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,092 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 1155
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by francois | last post: by
8 posts views Thread by SenthilVel | last post: by
33 posts views Thread by Ken | last post: by
16 posts views Thread by Sreekanth | last post: by
3 posts views Thread by Fred | last post: by
8 posts views Thread by Robin Tucker | last post: by
2 posts views Thread by archana | last post: by

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.