what is wrong with this code?

N

Nobody

I'm trying to write a class where I need to sort a list of TreeNode objects
in a certain way.

So I thought I'd use:

List<ListNodeData>

and then use the Sort method. I have the code working using SortedList
class, but its too slow since it sorts each time you insert. But just by
DECLARING the ListNodeData object, I get the infamous "exception has been
thrown by a target of an invocation" or whatever. I'm not even USING the
object and it throws an exception.

This is my ListNodeData class... it causes an exception by just being
defined in the namespace without even being used. Whats wrong with it?

public class ListNodeData : Object
{
public String strSortKey;
public TreeNode data;

public ListNodeData()
{
strSortKey = null;
data = null;
}

public override bool Equals(object obj)
{
if (obj.GetType() != this.GetType())
return false;
ListNodeData other = (ListNodeData)obj;
if ((other.strSortKey == strSortKey) && (other.data == data))
return true;
return false;
}

public override int GetHashCode()
{
return strSortKey.GetHashCode() ^ data.GetHashCode();
}

public override String ToString()
{
return data.ToString();
}

public ListNodeData Copy()
{
return (ListNodeData)MemberwiseClone();
}
}
 
R

rossum

I'm trying to write a class where I need to sort a list of TreeNode objects
in a certain way.

So I thought I'd use:

List<ListNodeData>

and then use the Sort method. I have the code working using SortedList
class, but its too slow since it sorts each time you insert. But just by
DECLARING the ListNodeData object, I get the infamous "exception has been
thrown by a target of an invocation" or whatever. I'm not even USING the
object and it throws an exception.

This is my ListNodeData class... it causes an exception by just being
defined in the namespace without even being used. Whats wrong with it?

Start with a completely empty class:

public class ListNodeData : Object
{
}

Compile and Run. If it fails then you have found your problem. If it
is OK then add a small piece of the class:

public class ListNodeData : Object
{
public String strSortKey;
public TreeNode data;
}

Compile and Run. If it fails then you have found your problem in the
bit you just added. If it is OK then add another a small piece of the
class. Repeat until you have either found the problem or got a
working copy of the class. Commenting out chunks of code works just
as well.

rossum


[snip code]



The ultimate truth is that there is no ultimate truth
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top