Compare object[] Not Working for Hashtable Keys

  • Thread starter Thread starter localhost
  • Start date Start date
L

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.
 
localhost said:
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.
 
Back
Top