Simple (I think) Inheritance question
-
You only need to define the constructors that you want, but be sure to call the base class's corresponding constructor like so:
public class MyTreeNode : TreeNode
{
public MyTreeNode(string text) : base(text)
{
// ...
}
}Also be sure that you cast the
TreeNode
from the tree to your class (ex:MyTreeNode
) when you need to access that string. TheTreeNodeCollection
will store a reference to your derivative class, but will only return aTreeNode
since that's all it deals with. That object is still an instance of your derivative class, though.Microsoft MVP, Visual C# My Articles
ok, thanks Heath. I assumed I still needed to define the constructors in my class, but wasn't sure...
-
I like to extend the TreeNode class through Inderitance to add a string property to the class. The TreeNode Class has 5 constructors, do I need to provide 5 contructors in my TreeNode class ? Is there some way I can pass a variable set of paramters to the base class's constructor ?
No, you don't have to provide 5 constructors for your TreeNode class. Since your extending the existing TreeNode class, all you have to do is supply your own constructor if one of the existing ones is not suitable. You could override the existing contructors to provide slightly different functionality, like filling in your extended fields with default data, or provide a completely new one so long as the signature of your constructor doesn't match an existing one. If you wanted to provide a constructor that took two String parameters and an existing TreeNode object, instead of a single String and TreeNode, you could because there is no matching constructor in the base class constructor list.
public TreeNode( string text, string altData, TreeNode[] children );
Now, if you wanted to provide a new constructor that takes the same type and same number of parameters, like:
public TreeNode( string text, TreeNode[] children );
and
public TreeNode( string altData, TreeNode[] children );
Then you would have to rearrange your parameter list because you can't ahve two constructors with the same signature, in this case
TreeNode(string,TreeNode[])
:public TreeNode( TreeNode[] children, string altData );
RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
No, you don't have to provide 5 constructors for your TreeNode class. Since your extending the existing TreeNode class, all you have to do is supply your own constructor if one of the existing ones is not suitable. You could override the existing contructors to provide slightly different functionality, like filling in your extended fields with default data, or provide a completely new one so long as the signature of your constructor doesn't match an existing one. If you wanted to provide a constructor that took two String parameters and an existing TreeNode object, instead of a single String and TreeNode, you could because there is no matching constructor in the base class constructor list.
public TreeNode( string text, string altData, TreeNode[] children );
Now, if you wanted to provide a new constructor that takes the same type and same number of parameters, like:
public TreeNode( string text, TreeNode[] children );
and
public TreeNode( string altData, TreeNode[] children );
Then you would have to rearrange your parameter list because you can't ahve two constructors with the same signature, in this case
TreeNode(string,TreeNode[])
:public TreeNode( TreeNode[] children, string altData );
RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
No, you don't have to provide 5 constructors for your TreeNode class. Since your extending the existing TreeNode class, all you have to do is supply your own constructor if one of the existing ones is not suitable. You could override the existing contructors to provide slightly different functionality, like filling in your extended fields with default data, or provide a completely new one so long as the signature of your constructor doesn't match an existing one. If you wanted to provide a constructor that took two String parameters and an existing TreeNode object, instead of a single String and TreeNode, you could because there is no matching constructor in the base class constructor list.
public TreeNode( string text, string altData, TreeNode[] children );
Now, if you wanted to provide a new constructor that takes the same type and same number of parameters, like:
public TreeNode( string text, TreeNode[] children );
and
public TreeNode( string altData, TreeNode[] children );
Then you would have to rearrange your parameter list because you can't ahve two constructors with the same signature, in this case
TreeNode(string,TreeNode[])
:public TreeNode( TreeNode[] children, string altData );
RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Dave thanks for the response, I'm not sure if this got sent to you b4 or not but here it is again...sorry if its duplicated... So now I'm alittle confused... I have the following code: MyTreeNode class implementation
namespace MyName
{
internal class MyTreeNode : System.Windows.Forms.TreeNode
{
private string ExtraLabel;
public string InternalLabel
{
get
{
return ExtraLabel;
}
set
{
ExtraLabel=value;
}
}internal MyTreeNode() { ExtraLabel=null; } }
}
Instanciation
MyTreeNode Node = (MyTreeNode)new TreeNode(Drive,IconIndex,IconIndex);
I get an invalid cast exception thrown...what am I doing wrong ?
-
Dave thanks for the response, I'm not sure if this got sent to you b4 or not but here it is again...sorry if its duplicated... So now I'm alittle confused... I have the following code: MyTreeNode class implementation
namespace MyName
{
internal class MyTreeNode : System.Windows.Forms.TreeNode
{
private string ExtraLabel;
public string InternalLabel
{
get
{
return ExtraLabel;
}
set
{
ExtraLabel=value;
}
}internal MyTreeNode() { ExtraLabel=null; } }
}
Instanciation
MyTreeNode Node = (MyTreeNode)new TreeNode(Drive,IconIndex,IconIndex);
I get an invalid cast exception thrown...what am I doing wrong ?
I think it's going to be more like this:
MyTreeNode Node = New MyTreeNode( _parameters_ );
And when you add it to the Nodes collection in your TreeView:
TreeView1.Nodes.Add( (TreeNode)MyTreeNode );
And when you try an access that node, say, in reference to the TreeView's SelectedNode property:
MyTreeNode myNode = (MyTreeNode)TreeView1.SelectedNode;
RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
I think it's going to be more like this:
MyTreeNode Node = New MyTreeNode( _parameters_ );
And when you add it to the Nodes collection in your TreeView:
TreeView1.Nodes.Add( (TreeNode)MyTreeNode );
And when you try an access that node, say, in reference to the TreeView's SelectedNode property:
MyTreeNode myNode = (MyTreeNode)TreeView1.SelectedNode;
RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
Well when I first created this class I tried your first suggestion;
MyTreeNode Node = new MyTreeNode(Drive, IconIndex,IconIndex);
but it would not compile, I got an error indicating that no constructor for MyTreeNode took 3 parameters (which according to my class is true...My constructor takes 0 parameters). Hence my initial question about needing to create default consrtuctors in MyTreeNode class duplicating the base TreeNode Constructors. This is why I'm confused. -
Well when I first created this class I tried your first suggestion;
MyTreeNode Node = new MyTreeNode(Drive, IconIndex,IconIndex);
but it would not compile, I got an error indicating that no constructor for MyTreeNode took 3 parameters (which according to my class is true...My constructor takes 0 parameters). Hence my initial question about needing to create default consrtuctors in MyTreeNode class duplicating the base TreeNode Constructors. This is why I'm confused.That's right, you have to supply your own constructor if your going to do this. The TreeNode class doesn't have a constructor that understands the parameters you gave it, so you have to supply one that takes those 3 parameters in your MyTreeNode class code, but you don't need to recreate the constructors of the base class, TreeNode. Just add you own contructor.
public MyTreeNode( string drive, Int32 iconIndex, Int32 iconIndex );
The Int32's should be whatever type your trying to pass into your constructor... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
That's right, you have to supply your own constructor if your going to do this. The TreeNode class doesn't have a constructor that understands the parameters you gave it, so you have to supply one that takes those 3 parameters in your MyTreeNode class code, but you don't need to recreate the constructors of the base class, TreeNode. Just add you own contructor.
public MyTreeNode( string drive, Int32 iconIndex, Int32 iconIndex );
The Int32's should be whatever type your trying to pass into your constructor... RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
ok, to recap: I need to duplicate the base class's contructors in my inherited class's implementation if I use them in my inherited class. If thats the case, and the TreeNode's constructor is suitable, then where does the following apply ? Dave Kreskowiak wrote: Since your extending the existing TreeNode class, all you have to do is supply your own constructor if one of the existing ones is not suitable.
-
ok, to recap: I need to duplicate the base class's contructors in my inherited class's implementation if I use them in my inherited class. If thats the case, and the TreeNode's constructor is suitable, then where does the following apply ? Dave Kreskowiak wrote: Since your extending the existing TreeNode class, all you have to do is supply your own constructor if one of the existing ones is not suitable.
When you need override the base class constructors and do some extra work that they don't. Your constructor will get called, then you do your extra processing or whatever, like setting default data in your version of the class, then you call the base class constructor with the same arguments that were sent to you. Now, if you need to get parameters that the base class constructors don't support, then you have to supply your own constructor with the parameters that you need. But, don't forget to call an appropriate base class constructor, so it can do the setup work it needs to do. RageInTheMachine9532 "...a pungent, ghastly, stinky piece of cheese!" -- The Roaming Gnome
-
I like to extend the TreeNode class through Inderitance to add a string property to the class. The TreeNode Class has 5 constructors, do I need to provide 5 contructors in my TreeNode class ? Is there some way I can pass a variable set of paramters to the base class's constructor ?
Guinness4Strength wrote: I like to extend the TreeNode class through Inderitance to add a string property to the class. If it's only adding a string property, why are you inheriting the TreeNode class at all? You can use TreeNode's Tag property to 'tag' (associate) any object with your node, including a string.