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

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

???
 
....sorry, I think it's the other way round:


i.e.

dim node as TreeNode should be dim node as MyTreeNode
 
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 |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'.
|
| ???
|
|
 
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.
 
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.
 
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 | 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.
|
|
|
 
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.
 
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 | >
| > 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.
|
|
|
 
Back
Top