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. Constructors

Constructors

Scheduled Pinned Locked Moved C#
question
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.
  • G Offline
    G Offline
    Ganjah786
    wrote on last edited by
    #1

    Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks

    L C H 3 Replies Last reply
    0
    • G Ganjah786

      Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks

      L Offline
      L Offline
      leppie
      wrote on last edited by
      #2

      No. But u can play some tricks and call different base class constructors. That mite work for your case. top secret xacc-ide 0.0.1

      1 Reply Last reply
      0
      • G Ganjah786

        Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks

        C Offline
        C Offline
        Colin Angus Mackay
        wrote on last edited by
        #3

        IIRC, the base constructor must be called in order to initialise things like private member variables that cannot be initialised from a derived class because it will be invisible to the derived class. Probably your best solution would be to create a protected constructor in the base that does nothing and have your derived class call it instead of the default constructor.


        "You can have everything in life you want if you will just help enough other people get what they want." --Zig Ziglar The Second EuroCPian Event will be in Brussels on the 4th of September Can't manage to P/Invoke that Win32 API in .NET? Why not do interop the wiki way! My Blog

        1 Reply Last reply
        0
        • G Ganjah786

          Hi, In an inherited class is it possible to stop the base class constructor from being called? Thanks

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

          Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.

          Microsoft MVP, Visual C# My Articles

          G A U 3 Replies Last reply
          0
          • H Heath Stewart

            Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.

            Microsoft MVP, Visual C# My Articles

            G Offline
            G Offline
            Ganjah786
            wrote on last edited by
            #5

            Tried that with the base classes constructor being private but then I can't inherit from it ( compile error ). eg. public class inheritance { public String strName; private inheritance() { showMessage(); } public virtual void showMessage() { MessageBox.Show("Base Class"); } } public class inheritance2: inheritance { public String strTest; public override void showMessage() { MessageBox.Show("inheritance2"); } }

            H 1 Reply Last reply
            0
            • H Heath Stewart

              Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.

              Microsoft MVP, Visual C# My Articles

              A Offline
              A Offline
              Alvaro Mendez
              wrote on last edited by
              #6

              Heath Stewart wrote: Also make the default constructor private in the base class. No, the default constructor would always be called, regardless. Heath Stewart wrote: Also make the default constructor private in the base class. If it's private in the base class, the derived class will not be able to call it automatically and the compiler will complain. The only solution is to add a protected constructor to the base class (assuming you can) and have the derived constructor call that explicitly. Regards, Alvaro


              Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.

              H 1 Reply Last reply
              0
              • H Heath Stewart

                Don't define a default constructor in your inheritted class. Also make the default constructor private in the base class.

                Microsoft MVP, Visual C# My Articles

                U Offline
                U Offline
                Uwe Keim
                wrote on last edited by
                #7

                Which idiot does vote your comments down to "1"? :mad: Im now voting "5" against when reading them :-) -- - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de - MSN Messenger: uwe_keim@hotmail.com

                H 1 Reply Last reply
                0
                • A Alvaro Mendez

                  Heath Stewart wrote: Also make the default constructor private in the base class. No, the default constructor would always be called, regardless. Heath Stewart wrote: Also make the default constructor private in the base class. If it's private in the base class, the derived class will not be able to call it automatically and the compiler will complain. The only solution is to add a protected constructor to the base class (assuming you can) and have the derived constructor call that explicitly. Regards, Alvaro


                  Give a man a fish, he owes you one fish. Teach a man to fish, you give up your monopoly on fisheries.

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

                  Really? Try compiling this and examining the IL with ildasm.exe or something:

                  using System;

                  public abstract class Test
                  {
                  private Test()
                  {
                  }

                  protected Test(string name)
                  {
                  }
                  

                  }

                  public class Test2 : Test
                  {
                  protected Test2(string name) : base(name)
                  {
                  }
                  }

                  You're right - the default ctor of the base class won't be called, but if it isn't private it will be called. There was no requirement in the original post for the default ctor of the base class to be called.

                  Microsoft MVP, Visual C# My Articles

                  1 Reply Last reply
                  0
                  • U Uwe Keim

                    Which idiot does vote your comments down to "1"? :mad: Im now voting "5" against when reading them :-) -- - Free Windows-based CMS: www.zeta-software.de/enu/producer/freeware/download.html - See me: www.magerquark.de - MSN Messenger: uwe_keim@hotmail.com

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

                    Thanks. :) I don't mind if people don't always agree with me, but when I'm right, I'm right - and in this case I'm right. See my reply to Alvaro.

                    Microsoft MVP, Visual C# My Articles

                    1 Reply Last reply
                    0
                    • G Ganjah786

                      Tried that with the base classes constructor being private but then I can't inherit from it ( compile error ). eg. public class inheritance { public String strName; private inheritance() { showMessage(); } public virtual void showMessage() { MessageBox.Show("Base Class"); } } public class inheritance2: inheritance { public String strTest; public override void showMessage() { MessageBox.Show("inheritance2"); } }

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

                      You can inherit, but only if you don't have a default constructor in your derivative class. If the base class's default ctor isn't private, it will be called. See my reply to Alvaro below for an example.

                      Microsoft MVP, Visual C# My Articles

                      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