Click here to Skip to main content
15,897,891 members
Home / Discussions / C#
   

C#

 
GeneralRe: Column Width Pin
Guffa6-Jun-06 8:44
Guffa6-Jun-06 8:44 
AnswerRe: Column Width Pin
yodern6-Jun-06 23:32
yodern6-Jun-06 23:32 
QuestionNeed help in messenger Pin
Musharaf Zaheer6-Jun-06 7:50
Musharaf Zaheer6-Jun-06 7:50 
GeneralRe: Need help in messenger Pin
Guffa6-Jun-06 11:15
Guffa6-Jun-06 11:15 
QuestionCheck the internet connection status Pin
ventomito6-Jun-06 7:31
ventomito6-Jun-06 7:31 
AnswerRe: Check the internet connection status Pin
Paul Brower6-Jun-06 10:38
Paul Brower6-Jun-06 10:38 
GeneralRe: Check the internet connection status Pin
ventomito7-Jun-06 2:21
ventomito7-Jun-06 2:21 
QuestionCasting from derived class to base class Pin
Melon006-Jun-06 7:02
Melon006-Jun-06 7:02 
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:

using System;<br />
using System.Collections.Generic;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace SimTree<br />
{<br />
    public class SimTreeNode : System.Windows.Forms.TreeNode<br />
    {<br />
        //public static explicit operator SimTreeNode(TreeNode tn)<br />
        //{<br />
<br />
        //}<br />
    }<br />
}


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.

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


Runtime Error:

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:

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. Mad | :mad:
AnswerRe: Casting from derived class to base class Pin
Wjousts6-Jun-06 7:13
Wjousts6-Jun-06 7:13 
AnswerRe: Casting from derived class to base class Pin
Josh Smith6-Jun-06 9:28
Josh Smith6-Jun-06 9:28 
AnswerRe: Casting from derived class to base class Pin
Guffa6-Jun-06 11:24
Guffa6-Jun-06 11:24 
AnswerRe: Casting from derived class to base class Pin
Ret Orrick6-Jun-06 15:49
Ret Orrick6-Jun-06 15:49 
QuestionPlease Help ! Pin
mostafa_h6-Jun-06 4:25
mostafa_h6-Jun-06 4:25 
AnswerRe: Please Help ! Pin
CodyGen6-Jun-06 5:45
CodyGen6-Jun-06 5:45 
GeneralRe: Please Help ! Pin
mostafa_h6-Jun-06 11:25
mostafa_h6-Jun-06 11:25 
Questionquestion about time and date format ? [modified] Pin
cmpeng346-Jun-06 4:20
cmpeng346-Jun-06 4:20 
AnswerRe: question about time and date format ? Pin
Dustin Metzgar6-Jun-06 4:39
Dustin Metzgar6-Jun-06 4:39 
AnswerRe: question about time and date format ? [modified] Pin
Wjousts6-Jun-06 4:40
Wjousts6-Jun-06 4:40 
QuestionDirect instantiation of Bitmap from unsafe memory Pin
Florian Storck6-Jun-06 4:10
Florian Storck6-Jun-06 4:10 
QuestionGet file that spawned the application. Pin
cmarcus6-Jun-06 4:01
cmarcus6-Jun-06 4:01 
AnswerRe: Get file that spawned the application. Pin
Stephan Samuel6-Jun-06 4:08
Stephan Samuel6-Jun-06 4:08 
GeneralRe: Get file that spawned the application. Pin
cmarcus6-Jun-06 5:58
cmarcus6-Jun-06 5:58 
AnswerRe: Get file that spawned the application. Pin
J4amieC6-Jun-06 4:11
J4amieC6-Jun-06 4:11 
AnswerRe: Get file that spawned the application - New Problem Pin
cmarcus6-Jun-06 8:44
cmarcus6-Jun-06 8:44 
QuestionShared Add-in for Excel 2003 and Garbage Collector Pin
Kadur6-Jun-06 3:55
Kadur6-Jun-06 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.