PC Review


Reply
Thread Tools Rate Thread

Compare object[] Not Working for Hashtable Keys

 
 
localhost
Guest
Posts: n/a
 
      13th Jan 2005


I have a HybridDictionary with a key containing an object array. It's
not matchng with the following code. What is a better way to do this.


HybridDictionary hd = new HybridDictionary(13);

object[] oneKey = new object[]{"a", 0 , "a" };
object[] oneValue = new object[]{"a", 0 , "a" };
object[] twoKey = new object[]{"b", 0 , "b" };
object[] twoValue = new object[]{"b", 0 , "b" };
hd.Add( oneKey , oneValue );
hd.Add( twoKey , twoValue );

object[] testKey = new object[]{"a", 0 , "a" };

IDictionaryEnumerator hdEnum = hd.GetEnumerator();
while( hdEnum.MoveNext() )
{
if ( testKey == (object[])hdEnum.Key )
{
Console.WriteLine("match.");
}
}


Thanks.
 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      13th Jan 2005
localhost <(E-Mail Removed)> wrote:
> I have a HybridDictionary with a key containing an object array. It's
> not matchng with the following code. What is a better way to do this.
>
> HybridDictionary hd = new HybridDictionary(13);
>
> object[] oneKey = new object[]{"a", 0 , "a" };
> object[] oneValue = new object[]{"a", 0 , "a" };
> object[] twoKey = new object[]{"b", 0 , "b" };
> object[] twoValue = new object[]{"b", 0 , "b" };
> hd.Add( oneKey , oneValue );
> hd.Add( twoKey , twoValue );
>
> object[] testKey = new object[]{"a", 0 , "a" };
>
> IDictionaryEnumerator hdEnum = hd.GetEnumerator();
> while( hdEnum.MoveNext() )
> {
> if ( testKey == (object[])hdEnum.Key )
> {
> Console.WriteLine("match.");
> }
> }


You're testing for reference equality, which certainly isn't true. You
need to write your own array comparison class.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HashTable stores whole object as Key NOT the result of calling GetHashCode on the object when you do myHashTable.Add? John Davis Microsoft Dot NET Framework 3 13th Apr 2006 02:31 PM
How to dertiminate if a sting in the keys of a hashtable ad Microsoft C# .NET 5 16th Nov 2005 11:58 AM
Compare values from objects stored in Hashtable Frank Microsoft C# .NET 3 2nd Nov 2004 07:58 PM
Hashtable without keys? cody Microsoft Dot NET Framework 32 26th Apr 2004 10:18 PM
Hashtable keys Jim Hooker Microsoft C# .NET 1 28th Jul 2003 11:33 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:37 PM.