Hashtable problem...
-
Current issue is making me crazy at the moment, there must be something I missed, 'cause it's not working as expected... I like to access a hash table key either by it's name or a class instance. To achieve this I create a new class (HashKey) which internally has a string called _name. When the hashtable calls HashKey.GetHashCode() it returns the HashCode of the internal wrapped string _name. Should be pretty much the same like I would use a literal string value key directly, right? so it must the be same if I add my value by a string key
HashTable myHashTable = new HashTable();
myHashTable.Add("keyName", 4);or I add my value by a string key wrapped in HashKey class.
HashKey myHashKey = new HashKey("keyName");
myHashTable.Add(myHashKey, 4);Now I should/wanna be able to read the value by literal string or by the HashKey instance.
object value = myHashTable["keyName"];
//
value = myHashTable[myHashKey];This doesn't work, but WHY? Here is the implemenation of HashKey class, which wraps the string and overrides GetHashCode and Equals methods.
public class HashKey
{
private string _name;public HashKey(string name) { \_name = name; } public override string ToString() { return \_name; } public override int GetHashCode() { return \_name.GetHashCode(); } public override bool Equals(object obj) { return \_name.Equals (obj); }
}
-
Current issue is making me crazy at the moment, there must be something I missed, 'cause it's not working as expected... I like to access a hash table key either by it's name or a class instance. To achieve this I create a new class (HashKey) which internally has a string called _name. When the hashtable calls HashKey.GetHashCode() it returns the HashCode of the internal wrapped string _name. Should be pretty much the same like I would use a literal string value key directly, right? so it must the be same if I add my value by a string key
HashTable myHashTable = new HashTable();
myHashTable.Add("keyName", 4);or I add my value by a string key wrapped in HashKey class.
HashKey myHashKey = new HashKey("keyName");
myHashTable.Add(myHashKey, 4);Now I should/wanna be able to read the value by literal string or by the HashKey instance.
object value = myHashTable["keyName"];
//
value = myHashTable[myHashKey];This doesn't work, but WHY? Here is the implemenation of HashKey class, which wraps the string and overrides GetHashCode and Equals methods.
public class HashKey
{
private string _name;public HashKey(string name) { \_name = name; } public override string ToString() { return \_name; } public override int GetHashCode() { return \_name.GetHashCode(); } public override bool Equals(object obj) { return \_name.Equals (obj); }
}
I copied your code and stepped through the debugger, and this is the solution, it works:
public override bool Equals(object obj) { if (obj is HashKey) return \_name.Equals(((HashKey)obj).\_name); else return \_name.Equals(obj); }
You need to cast the object when necessary and access the _name attribute directly. regards
-
I copied your code and stepped through the debugger, and this is the solution, it works:
public override bool Equals(object obj) { if (obj is HashKey) return \_name.Equals(((HashKey)obj).\_name); else return \_name.Equals(obj); }
You need to cast the object when necessary and access the _name attribute directly. regards
Hi Greeeg, Thanks for your fast reply, just added your code. Now it works to access the key with the HashKey instance
value = myHashTable[myHashKey];
but this scenario still doesn't work here
object value = myHashTable["keyName"];
Any other changes you made to the code, any ideas? myMsg.BehindDaKeys = "Jerry Maguire";
-
Hi Greeeg, Thanks for your fast reply, just added your code. Now it works to access the key with the HashKey instance
value = myHashTable[myHashKey];
but this scenario still doesn't work here
object value = myHashTable["keyName"];
Any other changes you made to the code, any ideas? myMsg.BehindDaKeys = "Jerry Maguire";
Strange, both work for me
Hashtable myHashTable = new Hashtable(); HashKey myHashKey = new HashKey("keyName"); myHashTable.Add(myHashKey, 4); value = myHashTable\["keyName"\]; value = null; value = myHashTable\[myHashKey\];
after both calls "value" is 4. I inserted the "value = null" to see whether the second call will change "value" at all. You might want to add another if-clause like
else if(obj is String) return _name.Equals((String)obj)
into the Equals method. regards -
Strange, both work for me
Hashtable myHashTable = new Hashtable(); HashKey myHashKey = new HashKey("keyName"); myHashTable.Add(myHashKey, 4); value = myHashTable\["keyName"\]; value = null; value = myHashTable\[myHashKey\];
after both calls "value" is 4. I inserted the "value = null" to see whether the second call will change "value" at all. You might want to add another if-clause like
else if(obj is String) return _name.Equals((String)obj)
into the Equals method. regardsIt doesn't work for me. I think it can't work because in case of asking
value = myHashTable["keyName"];
string.Equals() is called which is can never returns true when asked about equality with HashKey instance, right? myMsg.BehindDaKeys = "Jerry Maguire";
-
It doesn't work for me. I think it can't work because in case of asking
value = myHashTable["keyName"];
string.Equals() is called which is can never returns true when asked about equality with HashKey instance, right? myMsg.BehindDaKeys = "Jerry Maguire";
-
But you do an
object
comparison with the _name-string of your hashkey instance, so you compare two strings in case object is a string.Yep, but I guess the problem is that in case of the string hashtable key not the Hashkey.Equals() method is called. Instead it calls the string "keyName".Equals(hashKeyInstance) which returns false. So in case of asking the value with the string key, it works the other way round with calling Equals(). Is this correct? Thank you very much for your efforts. myMsg.BehindDaKeys = "Jerry Maguire";