K
Kevin Grigorenko
Thanks for anyone's help on this. First, here is my abbreviated class
structure:
class A {
protected obj myObj;
public override int GetHashCode() {
return myObj.GetHashCode();
}
public override bool Equals(object obj) {
if(obj is B) return myObj.Equals(obj);
return base.Equals(obj);
}
}
class B {
protected string path;
public override int GetHashCode() {
return path.GetHashCode();
}
}
class C : B {...}
class D : B {...}
Now, I have a Hashtable with instances of A as keys. But the Hashtable
isn't working.
Below is some code that got me to come here so baffled. parent is of
type A. The first line always returns null which is my problem. I have
proven that the correct instance of A is in the table during debug (and
the value is not null). The for loop is debug that goes through all of
the Hashtable keys and uses A's Equal() method (which goes to B's) and
it finds the key! Then, I have the key and I execute
(ListViewItem)treeToView[key] and I get null! So I'm walking through
the keys, find the one I want, then use the key from the table and look
up the value, and it's still not there (and the value is not null, I
have stepped to that line, and looked up the value myself in the
table).
Then I printed out the hashes of both the key and the original key
indexer and they are the same.
ListViewItem parentListItem = treeToView[parent] as ListViewItem;
foreach(Tree key in treeToView.Keys)
{
if(key == parent)
{
parentListItem = (ListViewItem)treeToView[key];
bool yes = treeToView.ContainsKey(key);
Console.WriteLine(key.GetHashCode() + "," + parent.GetHashCode());
break;
}
}
I'm at a loss here. The only thing I can see is that A's GetHashCode
calls GetHashCode on an object, not on a type of B. However, since
GetHashCode is virtual, if myObj is of type B then it will call B's
GetHashCode(), not System.Object()'s -- right?
Thanks so much!
Kevin
structure:
class A {
protected obj myObj;
public override int GetHashCode() {
return myObj.GetHashCode();
}
public override bool Equals(object obj) {
if(obj is B) return myObj.Equals(obj);
return base.Equals(obj);
}
}
class B {
protected string path;
public override int GetHashCode() {
return path.GetHashCode();
}
}
class C : B {...}
class D : B {...}
Now, I have a Hashtable with instances of A as keys. But the Hashtable
isn't working.
Below is some code that got me to come here so baffled. parent is of
type A. The first line always returns null which is my problem. I have
proven that the correct instance of A is in the table during debug (and
the value is not null). The for loop is debug that goes through all of
the Hashtable keys and uses A's Equal() method (which goes to B's) and
it finds the key! Then, I have the key and I execute
(ListViewItem)treeToView[key] and I get null! So I'm walking through
the keys, find the one I want, then use the key from the table and look
up the value, and it's still not there (and the value is not null, I
have stepped to that line, and looked up the value myself in the
table).
Then I printed out the hashes of both the key and the original key
indexer and they are the same.
ListViewItem parentListItem = treeToView[parent] as ListViewItem;
foreach(Tree key in treeToView.Keys)
{
if(key == parent)
{
parentListItem = (ListViewItem)treeToView[key];
bool yes = treeToView.ContainsKey(key);
Console.WriteLine(key.GetHashCode() + "," + parent.GetHashCode());
break;
}
}
I'm at a loss here. The only thing I can see is that A's GetHashCode
calls GetHashCode on an object, not on a type of B. However, since
GetHashCode is virtual, if myObj is of type B then it will call B's
GetHashCode(), not System.Object()'s -- right?
Thanks so much!
Kevin