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

Unable to cast object of type 'System.Windows.Forms.TreeNode' to type 'Renee.MyTreeNode'.?

P: n/a
I have the following class :

Public Class MyTreeNode
Inherits TreeNode
Dim mystring As String
End Class

Now, when I try to do this :

''''''''''''nodes is a TreeNodeCollection, s is string
dim node as TreeNode
node = CType(nodes.Add(s), MyTreeNode)

I receive :

Unable to cast object of type 'System.Windows.Forms.TreeNode' to type
'Renee.MyTreeNode'.

???
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
....sorry, I think it's the other way round:
i.e.

dim node as TreeNode should be dim node as MyTreeNode

Nov 21 '05 #2

P: n/a
Renee,
| node = CType(nodes.Add(s), MyTreeNode)
Nodes.Add adds a TreeNode object to the collection. If you want a MyTreeNode
in the collection you need to create & add a MyTreeNode to the collection.

Something like

Dim node As New MyTreeNode(s)
nodes.Add(node)

Just remember to create a new MyTreeNode for each node you are adding.

Elsewhere when you remove nodes from nodes, you can then cast them back to
MyTreeNode

Dim node As MyTreeNode
node = DirectCast(nodes(0), MyTreeNode)

Hope this helps
Jay

<Renee> wrote in message news:uz**************@TK2MSFTNGP15.phx.gbl...
|I have the following class :
|
| Public Class MyTreeNode
| Inherits TreeNode
| Dim mystring As String
| End Class
|
| Now, when I try to do this :
|
| ''''''''''''nodes is a TreeNodeCollection, s is string
| dim node as TreeNode
| node = CType(nodes.Add(s), MyTreeNode)
|
| I receive :
|
| Unable to cast object of type 'System.Windows.Forms.TreeNode' to type
| 'Renee.MyTreeNode'.
|
| ???
|
|
Nov 21 '05 #3

P: n/a
<Renee> wrote in message news:uz**************@TK2MSFTNGP15.phx.gbl...
Now, when I try to do this :

dim node as TreeNode
node = CType(nodes.Add(s), MyTreeNode)


So, you're asking the TreeNodeCollection to add a new TreeNode
based on the text supplied in s. This returns an object of Type
TreeNode. You're then trying to "cast" this into your [sub-]class,
MyTreeNode, which fails.

VB will not let you do it that way around. It /can/ cast one of your
subclasses (MyTreeNode) into the superclass (ancestor) type
(TreeNode), because it *knows* that everything in your subclass
is "compatible" with everything it knows about the superclass.
It /can't/ do this the other way around, because it cannot /guarantee/
that one of /its/ TreeNodes will support everything that you might
have added in your derived class, so it just won't let you do it.

You need to do things the /other/ way around.

Create a customised TreeNode in its entirety, then add this, lock
stock and barrel into the TreeView, /instead of/ the "standard"
TreeNode, something like

Dim newNode as New MyTreeNode( s )
nodes.Add( newNode )

HTH,
Phill W.
Nov 21 '05 #4

P: n/a
Jay, if you remember me from yesterday, then I will tell you that
you where right, there was an exception due to casting, the only
reason I didn't see it was that in my main loop I had a
"catch-do-nothing" which is a bad programming practice I
suppose. Phil has answered my question regarding the cast
of the /return type/ of the expression nodes.add(s)
[I think you didn't realise nodes.add(string) returns a TreeNode]

tnx.

Nov 21 '05 #5

P: n/a
Phil thanks, it works now, I did what you said.

tnx

Nov 21 '05 #6

P: n/a
Renee,
| Phil has answered my question regarding the cast
| of the /return type/ of the expression nodes.add(s)
| [I think you didn't realise nodes.add(string) returns a TreeNode]
Read my answer again, I do not discuss what Nodes.add(string) returns,
rather I discuss the type of node it creates & places in the collection. The
function returns the node that it creates & returns it. Hence I knew that it
returns TreeNode.

Hope this helps
Jay

<Renee> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl...
| Jay, if you remember me from yesterday, then I will tell you that
| you where right, there was an exception due to casting, the only
| reason I didn't see it was that in my main loop I had a
| "catch-do-nothing" which is a bad programming practice I
| suppose. Phil has answered my question regarding the cast
| of the /return type/ of the expression nodes.add(s)
| [I think you didn't realise nodes.add(string) returns a TreeNode]
|
| tnx.
|
|
|
Nov 21 '05 #7

P: n/a
>
Dim node As New MyTreeNode(s)
nodes.Add(node)
yes, I did this, I think I used also

MyTreeNode.text=s because the 'New' didn't like the 's' for some reason.

Just remember to create a new MyTreeNode for each node you are adding.

Elsewhere when you remove nodes from nodes, you can then cast them back to
MyTreeNode

Dim node As MyTreeNode
node = DirectCast(nodes(0), MyTreeNode)
I am not sure, but I think you may be able to get away without a cast,
because
I didn't use it and there was no error this time. Also, bear in mind I was
using
VS2005

Hope this helps
Jay


tnx.

Nov 21 '05 #8

P: n/a
Renee,
| yes, I did this, I think I used also
|
| MyTreeNode.text=s because the 'New' didn't like the 's' for some reason.

When you defined MyTreeNode you need to give it a constructor that accepts a
string. I presumed that you had such a constructor that used s for you new
value, something like:

Public Class MyTreeNode
Inherits TreeNode

Dim mystring As String

Public Sub New(mystring As String)
Me.mystring = mystring
End Sub

End Class

If s is the Text of the node rather then the mystring of the Node, then you
can do something like:

Public Class MyTreeNode
Inherits TreeNode

Dim mystring As String

Public Sub New(text As String)
MyBase.New(text)
End Sub

End Class

| I am not sure, but I think you may be able to get away without a cast,
| because
If you use Option Strict On, you need the cast. You are using Option Strict
On are you? Using Option Strict On causes a number of compile errors that
are easy to fix, rather then hard to find runtime errors...

If you use Option Strict Off, then I believe you can get away without a
cast, however you may be hiding subtle errors that are hard to find...

Hope this helps
Jay

<Renee> wrote in message news:uv**************@TK2MSFTNGP14.phx.gbl...
| >
| > Dim node As New MyTreeNode(s)
| > nodes.Add(node)
|
| yes, I did this, I think I used also
|
| MyTreeNode.text=s because the 'New' didn't like the 's' for some reason.
|
| >
| > Just remember to create a new MyTreeNode for each node you are adding.
| >
| > Elsewhere when you remove nodes from nodes, you can then cast them back
to
| > MyTreeNode
| >
| > Dim node As MyTreeNode
| > node = DirectCast(nodes(0), MyTreeNode)
|
| I am not sure, but I think you may be able to get away without a cast,
| because
| I didn't use it and there was no error this time. Also, bear in mind I was
| using
| VS2005
|
| >
| > Hope this helps
| > Jay
|
| tnx.
|
|
|
Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.