Casting to a derived TreeNode object (from a TreeViewEventArgs Node)

A

Andrew

created a custom class that is derived from TreeNode, let's call it
customTreeNode. I'm trying to use the TreeViewEventArgs (for the
AfterSelect event) but I cannot cast to my derived TreeNode. Here is
a snip...

---code snip---
private void _TreeView_AfterSelect(object sender, TreeViewEventArgs e)
{
customTreeNode foo = (customTreeNode)e.Node;
displayInfo(foo);
}

private void displayInfo(customTreeNode cTreeNode) {
//code
}
---code snip---

Following the example from MSDN,
(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html
/vbtsksubclassinglistitemortreenode.asp),
the MSDN example (at the bottom of the page) is almost exactly like
the above code.

At runtime, I get a InvalidCastException. What gives?

In the AfterSelect event handler method, if I do
'e.Node.GetType()'--it is, in fact, a customTreeNode. If I do not
attempt to cast to a customTreeNode and just try to pass it to
displayInfo, I get a compilation error because the compiler thinks
that e.Node is TreeNode, not a customTreeNode (expected).

Am I casting incorrectly? Is this a bug? Is the documentation shoddy?

Thanks,
Andrew
 
J

Jon Skeet

At runtime, I get a InvalidCastException. What gives?

In the AfterSelect event handler method, if I do
'e.Node.GetType()'--it is, in fact, a customTreeNode. If I do not
attempt to cast to a customTreeNode and just try to pass it to
displayInfo, I get a compilation error because the compiler thinks
that e.Node is TreeNode, not a customTreeNode (expected).

Am I casting incorrectly? Is this a bug? Is the documentation shoddy?

Are you loading the custom nodes via reflection by any chance? Many
cast errors which look like they shouldn't happen is because you're
trying to cast an instance of one type to another type with the same
name, but from a different assembly.
 
A

Andrew

Are you loading the custom nodes via reflection by any chance? Many
cast errors which look like they shouldn't happen is because you're
trying to cast an instance of one type to another type with the same
name, but from a different assembly.

Not sure what you mean by 'loading'. But, I load the nodes via the Add
method:
myTreeView.Nodes.Add(my_customTreeNode);

Add my customTreeNode class looks like this:

---customTreeNode class---

public customTreeNode : TreeNode {
private customData _myData;

public customTreeNode(customData myData) : base(myData.name,
(int)myData.status, (int)myData.status) { //to init a TreeNode with
different images based on the status
_myData = myData;
}

--end--
 
J

Jon Skeet

Andrew said:
Not sure what you mean by 'loading'. But, I load the nodes via the Add
method:
myTreeView.Nodes.Add(my_customTreeNode);

Add my customTreeNode class looks like this:

---customTreeNode class---

public customTreeNode : TreeNode {
private customData _myData;

public customTreeNode(customData myData) : base(myData.name,
(int)myData.status, (int)myData.status) { //to init a TreeNode with
different images based on the status
_myData = myData;
}

Hmmm - that should be fine then. What happens if you do:

Console.WriteLine (e.Node.GetType()==typeof(customTreeNode));

?
 
J

Jon Skeet

[Please don't mail me and post at the same time - it only confuses
things.]

Andrew said:
It returns false;

Right. In that case, check:

o The name (I think you've done this already)
o The assembly each type was loaded from, in terms of filename
o The assembly each type was loaded from, in terms of object reference

Can you provide a short but complete piece of code which demonstrates
the problem (and nothing else)?
 
C

Chris Hornberger

Your cast looks good. I use descendants of ListViewItems all the time.
Just got done working on one where ListViewMDBItem descended from
ListViewItem and contained all kinds of interesting tidbits about the
schema in the MDB file as well as the FullName to the mdb file. I've
done bunches of things like this and not had any problem, as long as
I'm watchful of the casts.

What he meant by "loading" your objects via reflection is if you were
sucking them from a DLL at runtime using the activator class. It
sounds like you're not so that problem isn't relavent here. Your code
should run fine.
 
J

Jon Skeet

Andrew said:
Yes, I attached the code. I tried to make it as short as possible.

<snip>

On my box that immediately bails out - and e.Node is *not* a
customTreeNode, it's a plain System.Windows.Forms.TreeNode.

That's because the "mommy" node (which is initially selected) is a
plain TreeNode. Changing the code for treeView1_AfterSelect to:

private void treeView1_AfterSelect(object sender,
System.Windows.Forms.TreeViewEventArgs e)
{
if (e.Node is customTreeNode)
{
customTreeNode cNode = (customTreeNode)e.Node;
this.textBox1.Text = cNode.myData;
}
}

it changes the text when I click on a child node, but not otherwise.

Given what you said previously about e.Node actually *being* a
customTreeNode when it's failing, I don't think I'm seeing the actual
problem here.
 

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