Casting subclass to its baseclass to retrieve its data members value

a1jit

Regular
Joined
Aug 19, 2005
Messages
89
Hi Guys,

Im not so sure why this code throws out an casting error since this feature can be implemented in vs 2003 but not in vs express 2005..

Basically i have a class which is inheriting the TreeNode class

Code:
   public class DGTree : TreeNode
    {
        private string groupName;

        public DGTree()
        {}

        public DGTree(string groupName, string text)
        {
            this.GroupName = groupName;
            this.Text = text;
            this.NavigateUrl = "index.aspx";
            this.PopulateOnDemand = true;
        }


        public string GroupName
        {
            get
            {
                return groupName;
            }
            set
            {
                groupName = value;
            }
        }

    }


Then, somewhere in the aspx page, im using the above class to create a treenode

Code:
        DGTree parent = new DGTree("CAnalysis", "Fabrication");
        TreeView1.Nodes.Add(parent);

        DGTree parent1 = new DGTree("CAnalysis", "Assembly");
        TreeView1.Nodes.Add(parent1);

Everything works fine..But when i try to retrieve the value of GroupName attached to each node using the event "TreeNodePopulate", i get some casting error. It seems like its not possible to cast the TreeNode Class to its subclass to retrieve its member values (GroupName)

Error: Unable to cast object of type 'System.Web.UI.WebControls.TreeNode' to type 'DGTree'.


Code:
    protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {

        DGTree node = (DGTree)e.Node;
        populateChildren(node.GroupName.ToString());  
    }

Im not sure why, but this same approach works in the .net 2003, but no idea why it doesnt work in express edition 2005, am i missing something?
 
Last edited:
PlausiblyDamp said:
If you put a breakpoint on the line
C#:
DGTree node = (DGTree)e.Node;
what type of object does it think e.Node is?


Its of type TreeNode..But in previous version, im able to do this kind of casting..because im just converting its type to its subclass which is the concept of OOP..Am i right..I dont think there is a syntax issue with this statement..What is your opinion on this..This type of casting is possible right?
 
I must admit I only have experience of .Net 1.1 and VS 2003, but as you say this type of conversion is certainly possible in those and I cannot see any conceptual reasons for them to have taken out such functionality. The only thing I can think of (and I highly doubt its your problem) is that there are two TreeNode objects in .Net, one in the System.Forms.Controls. Namespace, and the other is in the Web namespace as you say. Are you certain the DGTree object is inheriting from the right one? I would imagine if this was wrong you would encounter a problem before this point, but I just thought I'd throw it out there anyway.
 
A random guess but seeing how you're accessing the treenode for reading on a postback.. it must reconstruct the treenode and maybe that's where the issue is... It's maybe using it's builtin classes to reconstruct the treenode.
 
Hi Guys,

thanks a lot for the reply, im not sure why the casting is still not working..

Actually this is wht im trying to accomplish. Im creating a few custom classes
to represent each level of nodes..So im just trying to detect the type of node when the user try to expand the tree. So according to the type of node, then i know which value should be loaded as its child.

I dont want to use the depth level as its very rigid, and its not good if there is a change need to be implemented in future.

So i want to create something similar to the link shown below in asp.net. If anyone has some idea, hope you dont mind sharing it with me. Thanks al ot

http://www.devcity.net/Articles/23/1/custom_treeview.aspx
 
Actually, your problem is that you are trying to cast a TreeNode instance into a DGTree instance when the TreeNode instance is not really a DGTree instance. What I mean by this is that the TreeNode instance does not hold the signature of the DGTree class so, since there are no type converters defined, the CLR is going to throw an exception. Basically, DGTree is always a TreeNode but a TreeNode is not always a DGTree. If you try casting a TreeNode that is not a DGTree into a DGTree then you are going to get the error you experienced.

C#:
TreeNode node = treeView1.SelectedNode;

if (node is DGTree)
{
    // both are valid
    DGTree downcast = (DGTree)node;

    TreeNode upcast = (TreeNode)downcast;
}
else
{

    // invalid
    DGTree downcast = (DGTree)node;

}
 
Back
Top