Home All Groups Group Topic Archive Search About

Compare object[] Not Working for Hashtable Keys

Author
13 Jan 2005 6:43 PM
localhost
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.

Author
13 Jan 2005 10:23 PM
Jon Skeet [C# MVP]
localhost <primpi***@cohort.ces> wrote:
Show quote
> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

AddThis Social Bookmark Button