473,508 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
8 6492
....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
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
<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
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
Phil thanks, it works now, I did what you said.

tnx

Nov 21 '05 #6
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
>
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
5591
by: Fidias Gil de Montes | last post by:
In a Distributed Windows application, I receive the following message when the client calls the server: ************** Exception Text ************** System.InvalidCastException: Unable to cast...
7
6160
by: Andrew | last post by:
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. ...
4
3810
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
2
1493
by: Rene | last post by:
Hi all i need some help. i try to send mail with the following code. Private Sub LinkButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LinkButton1.Click Dim...
3
6660
by: GreyAlien007 | last post by:
I extended the class TreeNode to add some properties of my liking. Anyway, no problems there, I can add my derived TreeNode into TreeNodeCollections and use the properties etc. However, when I...
1
5143
by: Ratz | last post by:
Hello everyone! I'm new to this Forum! I've spent about 56 hours trying to solve a .NET VB Runtime error while Using a program.Ive contacted the developer of the program and he could not figure out...
0
4589
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Hi, Thanks in advance for reading this. Not sure where to post this question, but I hope someone in here can help. Trying to write to Event Log in VS 2005 (.NET 2.0) using Enterprise Library...
8
6709
by: bryan.kardisco | last post by:
I'm currently trying to extend the TreeViewItem class so I can attach some additional attributes to the item. I created a custom class class myTreeNode : System.Windows.Controls.TreeViewItem...
2
8735
by: vinrin | last post by:
Thank for your answer. :-) call CheckEmptyNode (treeview) public void CheckEmptyNode( Object N ) { Microsoft.Web.UI.WebControls.TreeNode menuNode = null; ...
0
7225
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7123
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7382
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7042
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.