Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Go Back   Code Forums > Application and Web Development > MS Technologies ( ASP, VB, C#, .NET )
User Name
Password

Reply
 
LinkBack Thread Tools Display Modes
Old 06-06-2006, 08:34 AM   #1 (permalink)
Melon00
Trent
 
Join Date: Jun 2006
Location: Ames, IA
Posts: 4
Melon00 is on a distinguished road
Question [C#] Casting from derived class to base class

I have a class I am calling SimTreeNode which is a System.Windows.Forms.TreeNode type. I am planning on using this class within my TreeView class.

Class Definition:

Code:
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace SimTree { public class SimTreeNode : System.Windows.Forms.TreeNode { //public static explicit operator SimTreeNode(TreeNode tn) //{ //} } }
As you can see, I don't do anything more or less than the TreeNode class.

The problem is, when I try to use my SimTreeNode in place of any TreeNode in my TreeView class, it throws runtime errors.

Here is the snippet of code I am doing the conversion.
Code:
using System.Collections; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Serialization; using SimTree.Properties; using System.Runtime.InteropServices; namespace SimTree { public partial class SimTree : System.Windows.Forms.TreeView { private void SimTree_ItemDrag(object sender, ItemDragEventArgs e) { // Do not allow anything other than devices to be dragged Point pt = this.PointToClient(Control.MousePosition); SimTreeNode tn = (SimTreeNode)this.GetNodeAt(pt); // <--- Runtime error here ... } } }
Runtime Error:

Code:
Unable to cast object of type 'System.Windows.Forms.TreeNode' to type 'SimTree.SimTreeNode'.
I would like to know why I cant caste my derived type to my base type.

According to a million online forums, I don't need to make an explicit operator to caste to/from base types. I actually tried to make an explicit caste operator if you look at my comments in the SimTree.SimTreeNode class, but gave me a compiler error:

Code:
Error 1 'SimTree.SimTreeNode.explicit operator SimTree.SimTreeNode(System.Windows.Forms.TreeNode)': user-defined conversion to/from base class C:\...\SimTree\SimTreeNode.cs 10 23 SimTree
This really doesn't make any sense to me as the compiler SHOULDN'T have anyway of telling how to convert a base type to a derived type without me telling it explicitly.

I am hitting brick walls left and right with this simple thing I want to do.
__________________
Melon00 is offline   Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Class using a specific instance of another class? is this possible? abs Standard C, C++ 5 02-08-2005 03:12 PM
Accessing functions from base class dirs Standard C, C++ 80 05-20-2004 04:47 AM
Java Class Basics: Static Methods bdl Java 0 02-29-2004 02:25 PM


All times are GMT -8. The time now is 09:43 PM.


Powered by vBulletin Version 3.6.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle