Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Simple (I think) Inheritance question

Simple (I think) Inheritance question

Scheduled Pinned Locked Moved C#
questionoop
13 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Guinness4Strength

    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 ?

    J Offline
    J Offline
    Judah Gabriel Himango
    wrote on last edited by
    #2

    Guinness4Strength wrote: do I need to provide 5 contructors in my TreeNode class No, and you'll still be able to call those 5 constructors on your inherited class. Guinness4Strength wrote: Is there some way I can pass a variable set of paramters to the base class's constructor ? Why? You can provide your own constructors that call the base constructors, and optionally, set various properties of the TreeNode while in the body of your constructor. #include "witty_sig.h"

    1 Reply Last reply
    0
    • G Guinness4Strength

      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 ?

      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #3

      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. The TreeNodeCollection will store a reference to your derivative class, but will only return a TreeNode since that's all it deals with. That object is still an instance of your derivative class, though.

      Microsoft MVP, Visual C# My Articles

      G 1 Reply Last reply
      0
      • H Heath Stewart

        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. The TreeNodeCollection will store a reference to your derivative class, but will only return a TreeNode since that's all it deals with. That object is still an instance of your derivative class, though.

        Microsoft MVP, Visual C# My Articles

        G Offline
        G Offline
        Guinness4Strength
        wrote on last edited by
        #4

        ok, thanks Heath. I assumed I still needed to define the constructors in my class, but wasn't sure...

        1 Reply Last reply
        0
        • G Guinness4Strength

          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 ?

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #5

          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

          B G 2 Replies Last reply
          0
          • D Dave Kreskowiak

            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

            B Offline
            B Offline
            BrcKcc
            wrote on last edited by
            #6

            By the way, if you are going to be doing a lot with tree controls, you might look at the Infragistics tree control. It's a bit expensive, but it has a lot more flexibility than the Microsoft standard tree control. - Bruce BRCKCC

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              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

              G Offline
              G Offline
              Guinness4Strength
              wrote on last edited by
              #7

              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 ?

              D 1 Reply Last reply
              0
              • G Guinness4Strength

                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 ?

                D Offline
                D Offline
                Dave Kreskowiak
                wrote on last edited by
                #8

                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

                G 1 Reply Last reply
                0
                • D Dave Kreskowiak

                  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

                  G Offline
                  G Offline
                  Guinness4Strength
                  wrote on last edited by
                  #9

                  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.

                  D 1 Reply Last reply
                  0
                  • G Guinness4Strength

                    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.

                    D Offline
                    D Offline
                    Dave Kreskowiak
                    wrote on last edited by
                    #10

                    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

                    G 1 Reply Last reply
                    0
                    • D Dave Kreskowiak

                      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

                      G Offline
                      G Offline
                      Guinness4Strength
                      wrote on last edited by
                      #11

                      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.

                      D 1 Reply Last reply
                      0
                      • G Guinness4Strength

                        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.

                        D Offline
                        D Offline
                        Dave Kreskowiak
                        wrote on last edited by
                        #12

                        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

                        1 Reply Last reply
                        0
                        • G Guinness4Strength

                          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 ?

                          B Offline
                          B Offline
                          Baris Kurtlutepe
                          wrote on last edited by
                          #13

                          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.

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups