Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 2nd, 2004, 04:27 PM
Authorized User
 
Join Date: Aug 2003
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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...






Similar Threads
Thread Thread Starter Forum Replies Last Post
Inheritance magagulad Visual Basic 2005 Basics 1 May 12th, 2008 07:51 AM
Inheritance michaelcode ASP.NET 2.0 Basics 5 September 26th, 2006 01:40 PM
Need help with inheritance filip BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 1 August 25th, 2006 09:38 PM
c# inheritance bhohman C# 2 March 26th, 2004 01:47 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.