By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,335 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,335 IT Pros & Developers. It's quick & easy.

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

P: n/a
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/de...-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
Nov 13 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Andrew <ac*********@pherret.com> wrote:

<snip>
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #2

P: n/a
> 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--
Nov 13 '05 #3

P: n/a
Andrew <ac*********@pherret.com> wrote:
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;
}


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

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

?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #4

P: n/a
> Hmmm - that should be fine then. What happens if you do:

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

?


It returns false;
Nov 13 '05 #5

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

Andrew <ac*********@pherret.com> wrote:
Hmmm - that should be fine then. What happens if you do:

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

?


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)?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #6

P: n/a
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.

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

Thanks,
Andrew

Nov 13 '05 #7

P: n/a
Andrew <ac*********@pherret.com> wrote:
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)?


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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 13 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.