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. Inheritance and constructors

Inheritance and constructors

Scheduled Pinned Locked Moved C#
questionoophelp
10 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.
  • D Offline
    D Offline
    Dewald
    wrote on last edited by
    #1

    Hi all, I'm having a hard time getting my head around this() and base(), hoping someone can help. Let's say I have classB which inherits classA both have a constructor and an overloaded constructor.

    class classA
    {
    public classA()
    {
    //some code
    }
    public classA(int i)
    {
    //some code
    }
    }

    class classB
    {
    public classB()
    {
    //some code
    }
    public classB(int i)
    {
    //some code
    }
    }

    How do I get the overloaded constructor of classB to first call the default constructor of classB and then the similar overloaded constructor of the base class, classA? I seem to be able to have it call either the default constructor of classB OR the overloaded constructor of classA by declaring the overloaded constructor of classB as public classB(int i) : this() or public classB(int i) : base(i) but how do I get it to call both?

    N P F L 4 Replies Last reply
    0
    • D Dewald

      Hi all, I'm having a hard time getting my head around this() and base(), hoping someone can help. Let's say I have classB which inherits classA both have a constructor and an overloaded constructor.

      class classA
      {
      public classA()
      {
      //some code
      }
      public classA(int i)
      {
      //some code
      }
      }

      class classB
      {
      public classB()
      {
      //some code
      }
      public classB(int i)
      {
      //some code
      }
      }

      How do I get the overloaded constructor of classB to first call the default constructor of classB and then the similar overloaded constructor of the base class, classA? I seem to be able to have it call either the default constructor of classB OR the overloaded constructor of classA by declaring the overloaded constructor of classB as public classB(int i) : this() or public classB(int i) : base(i) but how do I get it to call both?

      N Offline
      N Offline
      netJP12L
      wrote on last edited by
      #2

      First of all the proper way to inherit is : public ClassB:ClassA { } Secondly, when you are instianting ClassB it calls the parent constructor first and then instiante the derived class. For instance ClassB cb = new ClassB()---> it calls the Parent no value(default constructor) first and then public classB() { } Thirdly, ClassB cb = new Classb(2)-->it calls the Parent value constructor in ur case is public classA(int i) { } and then the dervied class constructor public classB(int i) { //some code } hope it may help

      D 1 Reply Last reply
      0
      • D Dewald

        Hi all, I'm having a hard time getting my head around this() and base(), hoping someone can help. Let's say I have classB which inherits classA both have a constructor and an overloaded constructor.

        class classA
        {
        public classA()
        {
        //some code
        }
        public classA(int i)
        {
        //some code
        }
        }

        class classB
        {
        public classB()
        {
        //some code
        }
        public classB(int i)
        {
        //some code
        }
        }

        How do I get the overloaded constructor of classB to first call the default constructor of classB and then the similar overloaded constructor of the base class, classA? I seem to be able to have it call either the default constructor of classB OR the overloaded constructor of classA by declaring the overloaded constructor of classB as public classB(int i) : this() or public classB(int i) : base(i) but how do I get it to call both?

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Try the following:

        class classA
        {
        public classA() : this(0)
        {
        //some code
        }
        public classA(int i)
        {
        //some code
        }
        }
        class classB : classA
        {
        public classB() : base()
        {
        //some code
        }
        public classB(int i) : base(i)
        {
        //some code
        }
        }

        With this, calling the classB constructors will result in the appropriate classA constructors being called. If however, you want to explicitly perform some code in response to the constructors, I would create an OnInit method, and call it from public classA(int i). This means that all of the paths through to it will be handled.

        Deja View - the feeling that you've seen this post before.

        My blog | My articles

        N D 2 Replies Last reply
        0
        • D Dewald

          Hi all, I'm having a hard time getting my head around this() and base(), hoping someone can help. Let's say I have classB which inherits classA both have a constructor and an overloaded constructor.

          class classA
          {
          public classA()
          {
          //some code
          }
          public classA(int i)
          {
          //some code
          }
          }

          class classB
          {
          public classB()
          {
          //some code
          }
          public classB(int i)
          {
          //some code
          }
          }

          How do I get the overloaded constructor of classB to first call the default constructor of classB and then the similar overloaded constructor of the base class, classA? I seem to be able to have it call either the default constructor of classB OR the overloaded constructor of classA by declaring the overloaded constructor of classB as public classB(int i) : this() or public classB(int i) : base(i) but how do I get it to call both?

          F Offline
          F Offline
          Frank Horn
          wrote on last edited by
          #4

          but how do I get it to call both? You don't, cause you can only have on base instance, so you can't call two constructors. You'll have to put the code from both base class constructors into two protected methods and call each from the respective base class constructor, and call both from the derived class constructor.

          D 1 Reply Last reply
          0
          • D Dewald

            Hi all, I'm having a hard time getting my head around this() and base(), hoping someone can help. Let's say I have classB which inherits classA both have a constructor and an overloaded constructor.

            class classA
            {
            public classA()
            {
            //some code
            }
            public classA(int i)
            {
            //some code
            }
            }

            class classB
            {
            public classB()
            {
            //some code
            }
            public classB(int i)
            {
            //some code
            }
            }

            How do I get the overloaded constructor of classB to first call the default constructor of classB and then the similar overloaded constructor of the base class, classA? I seem to be able to have it call either the default constructor of classB OR the overloaded constructor of classA by declaring the overloaded constructor of classB as public classB(int i) : this() or public classB(int i) : base(i) but how do I get it to call both?

            L Offline
            L Offline
            laserbaronen
            wrote on last edited by
            #5

            class classB : classA
            {
            public classB() : this(5)
            {
            //some code
            }
            public classB(int i) : base("laser")
            {
            //some code
            }
            }

            calling classB() will run both


            betonglasermur.FeedDwarf(pur_is, 17);
            ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

            Morgonen är tröttmans mecka

            D 1 Reply Last reply
            0
            • P Pete OHanlon

              Try the following:

              class classA
              {
              public classA() : this(0)
              {
              //some code
              }
              public classA(int i)
              {
              //some code
              }
              }
              class classB : classA
              {
              public classB() : base()
              {
              //some code
              }
              public classB(int i) : base(i)
              {
              //some code
              }
              }

              With this, calling the classB constructors will result in the appropriate classA constructors being called. If however, you want to explicitly perform some code in response to the constructors, I would create an OnInit method, and call it from public classA(int i). This means that all of the paths through to it will be handled.

              Deja View - the feeling that you've seen this post before.

              My blog | My articles

              N Offline
              N Offline
              N a v a n e e t h
              wrote on last edited by
              #6

              Pete O'Hanlon wrote:

              public classB() : base() { //some code }

              I think the call to base() is not needed here. When classB is instantiated, parameterless constructor of base class will be called.

              All C# applications should call Application.Quit(); in the beginning to avoid any .NET problems.- Unclyclopedia How to use google | Ask smart questions

              1 Reply Last reply
              0
              • N netJP12L

                First of all the proper way to inherit is : public ClassB:ClassA { } Secondly, when you are instianting ClassB it calls the parent constructor first and then instiante the derived class. For instance ClassB cb = new ClassB()---> it calls the Parent no value(default constructor) first and then public classB() { } Thirdly, ClassB cb = new Classb(2)-->it calls the Parent value constructor in ur case is public classA(int i) { } and then the dervied class constructor public classB(int i) { //some code } hope it may help

                D Offline
                D Offline
                Dewald
                wrote on last edited by
                #7

                Are you sure about that second case? As far as I understand (and can reproduce), if you instantiate ClassB cb = new ClassB(2) it calls the default constructor of ClassA first and then the overloaded constructor of ClassB. It never calls the overloaded constructor of ClassA as you say. This is of course if you don't specify for the constructors whether they should be calling other constructors. If I wanted the overloaded constructor of ClassB to first call the overloaded constructor of ClassA as opposed to the default constructor of ClassA, I would have had to specify it as:

                public ClassB(int i) : base(i)
                {
                //some code
                }

                Am I missing something?

                1 Reply Last reply
                0
                • P Pete OHanlon

                  Try the following:

                  class classA
                  {
                  public classA() : this(0)
                  {
                  //some code
                  }
                  public classA(int i)
                  {
                  //some code
                  }
                  }
                  class classB : classA
                  {
                  public classB() : base()
                  {
                  //some code
                  }
                  public classB(int i) : base(i)
                  {
                  //some code
                  }
                  }

                  With this, calling the classB constructors will result in the appropriate classA constructors being called. If however, you want to explicitly perform some code in response to the constructors, I would create an OnInit method, and call it from public classA(int i). This means that all of the paths through to it will be handled.

                  Deja View - the feeling that you've seen this post before.

                  My blog | My articles

                  D Offline
                  D Offline
                  Dewald
                  wrote on last edited by
                  #8

                  Thanks, but this won't exactly do what I'm looking for. I'm looking for a way to have an instantiation of ClassB cb = new ClassB(1) make a call to both constructors of ClassB as well as the default constructor of ClassA. The way you suggest would result in an instantiation as above to only call the overloaded constructors of both classes, but not the default onstructor of ClassA. Not to worry though. The more I think about it, the more I realise that I'm trying to use OOP in a way it's not meant to be used. I've found another way to achieve what I want anyway. Also, I think your suggestion of an OnInit method makes better sense as well. Thanks again.

                  1 Reply Last reply
                  0
                  • F Frank Horn

                    but how do I get it to call both? You don't, cause you can only have on base instance, so you can't call two constructors. You'll have to put the code from both base class constructors into two protected methods and call each from the respective base class constructor, and call both from the derived class constructor.

                    D Offline
                    D Offline
                    Dewald
                    wrote on last edited by
                    #9

                    Makes sense. I realised afterwards that this is really what OOP is all about. Thanks for the response.

                    1 Reply Last reply
                    0
                    • L laserbaronen

                      class classB : classA
                      {
                      public classB() : this(5)
                      {
                      //some code
                      }
                      public classB(int i) : base("laser")
                      {
                      //some code
                      }
                      }

                      calling classB() will run both


                      betonglasermur.FeedDwarf(pur_is, 17);
                      ProcessStartupInfo.AintNotCreateNoWindow = (false && !true) != (true || false) ? false == true ? true : false : (true != false && false);

                      Morgonen är tröttmans mecka

                      D Offline
                      D Offline
                      Dewald
                      wrote on last edited by
                      #10

                      Huh? You've got me nicely confused here. Are you being serious?

                      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