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#
csharphelpquestion
3 Posts 2 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.
  • A Offline
    A Offline
    AJ123
    wrote on last edited by
    #1

    I am looking at orgainsing a custom constructor in one of my classes so that it calls through to my default constuctor. I understand this is done as follows: Public TestClass() { //Default Constructor } Public TestClass(int testValue) :this() { } The problem is i also want to call a base constructor, so i effectively want : Public TestClass(int testValue) :base(int testValue2) : this() Which is not valid syntax. Is there an easy way of capturing this in C#? Cheers AJ

    C 1 Reply Last reply
    0
    • A AJ123

      I am looking at orgainsing a custom constructor in one of my classes so that it calls through to my default constuctor. I understand this is done as follows: Public TestClass() { //Default Constructor } Public TestClass(int testValue) :this() { } The problem is i also want to call a base constructor, so i effectively want : Public TestClass(int testValue) :base(int testValue2) : this() Which is not valid syntax. Is there an easy way of capturing this in C#? Cheers AJ

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

      AJ123 wrote:

      Is there an easy way of capturing this in C#?

      You put the reference to the base on the default constructor. This chains the constructors together. For example:

      class Program
      {
          static void Main(string\[\] args)
          {
              B b = new B(12);
          }
      }
      
      class A
      {
          public A()
          {
              Console.WriteLine("I'm in the constructor of A");
          }
      }
      
      class B : A
      {
          public B() : base()
          {
              Console.WriteLine("I'm in the constructor of B");
          }
      
          public B(int someValue) : this()
          {
              Console.WriteLine("I'm in the constructor of B({0})", someValue);
          }
      }
      

      The output is

      I'm in the constructor of A
      I'm in the constructor of B
      I'm in the constructor of B(12)

      ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

      A 1 Reply Last reply
      0
      • C Colin Angus Mackay

        AJ123 wrote:

        Is there an easy way of capturing this in C#?

        You put the reference to the base on the default constructor. This chains the constructors together. For example:

        class Program
        {
            static void Main(string\[\] args)
            {
                B b = new B(12);
            }
        }
        
        class A
        {
            public A()
            {
                Console.WriteLine("I'm in the constructor of A");
            }
        }
        
        class B : A
        {
            public B() : base()
            {
                Console.WriteLine("I'm in the constructor of B");
            }
        
            public B(int someValue) : this()
            {
                Console.WriteLine("I'm in the constructor of B({0})", someValue);
            }
        }
        

        The output is

        I'm in the constructor of A
        I'm in the constructor of B
        I'm in the constructor of B(12)

        ColinMackay.net Scottish Developers are looking for speakers for user group sessions over the next few months. Do you want to know more?

        A Offline
        A Offline
        AJ123
        wrote on last edited by
        #3

        Thanks Colin, seems obvious now. I was actually wanting the custom constructor of class B to call a different base class constructor than B's default constructor. Which is just been plain silly... :)

        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