inheritance trouble!!
I am using a treeview web control for browsing/selecting a file on the webserver(actually the webserver only has the directory structure info, the actual files are on another server).
Instead of using TreeNode objects in my TreeView, I am using an object I derive from TreeNode.
public class MyTreeNode: TreeNode
{
public MyTreeNode(string type, string id, string textin, string infoIn)
{
this.Type = type; //TreeNodeType for image icon
this.ID = id;
this.text = textin;
this.someMoreInfo = infoIn;
}
private string someMoreInfo;
}
I add these nodes to my tree, and it's all good...I can see the icons for each type of TreeNode, I can select and identify the selected nodes and such.
To find out which node was selected, I can use the following methods
of TreeView control:
//get selected node index
string index = this.MyTreeView.SelectedNodeIndex;
TreeNode node = this.TreeViewDir.GetNodeFromIndex(index);
This function is designed to return TreeNode and not MyTreeNode (it's not my function, but a part of TreeView class). However, the reference it returns still points to MyTreeNode, and I'm able to downcast node to MyTreeNode, as in
(MyTreeNode)node
However, I lose the info stored in the attribute I added to my derived class (someMoreInfo).
((MyTreeNode)node).someMoreInfo is now null, even though it was not null when the node got added to the tree. If my downcast is safe, since I know I added a derived type of object, should'nt I be able to retrieve all the info of the derived object after downcasting?
The base class TreeNode still has its attribute straight. i.e. ID, Text, and Type;
Would appreciate any advice...
|