locked
Constructor Chaining in C#

    Question

  • Dear All,

    I am actually new to Object Orientated Programming. I was just studing about Constructors  and I came across the concept of Constructor Chaining.Can some 1
    explain me about what is this constructor chaining.I know it means that we can call one constructor from another constructor. I have tried 1 example which is
    as follows:


    class Class1

    {

    private string e_name,e_JobTiltle,e_Address ;

    private Class1(string name,string Title)

    {
    e_name =name;
    e_JobTiltle=Title;
    }

    private Class1(string name,string Title,string Address)

    {
    e_name =name;
    e_JobTiltle=Title;
    e_Address = Address;
    }

    Now how can I implement Constructor chaining in the above example.Sorry its a beginner level question. Any help will be greatly appreciated.

    Thanking you in Anticipation.

    cheers,
    Sam

    Friday, September 30, 2005 3:34 PM

Answers

  • the 'chain constructors' ideas are that you write as little code as possible, and, cope with the fact that C# doesnt allow 'default arguments'. Also, the ctors must be 'public', or the world will not see them.

    Therefore, one writes

    public Class1(string name,string Title,string Address){   //   the "second" ctor 
       e_name =name;
       e_JobTiltle=Title;
       e_Address = Address;
    }

    and, the first ctor becomes

    public Class1(string name,string Title) : this(name, Title, "my default address")
    {}

    Friday, September 30, 2005 3:56 PM

All replies

  • You dont have to change anything in the above code.

    Basically constructor chaining is where a subclass calls its superclasses constructor which subsequentally calls its superclasses constrctor and so on.

    So if you were to create another class that inherits from the above base class then the derived class would be guaranteed to call the base classes constructor.

    So every derived class from there on would call its superclasses constructor until it gets the ther original base class.

    Thats constructor chaining.

    Friday, September 30, 2005 3:48 PM
  • the 'chain constructors' ideas are that you write as little code as possible, and, cope with the fact that C# doesnt allow 'default arguments'. Also, the ctors must be 'public', or the world will not see them.

    Therefore, one writes

    public Class1(string name,string Title,string Address){   //   the "second" ctor 
       e_name =name;
       e_JobTiltle=Title;
       e_Address = Address;
    }

    and, the first ctor becomes

    public Class1(string name,string Title) : this(name, Title, "my default address")
    {}

    Friday, September 30, 2005 3:56 PM
  • Dear Peter,

    Thanks for the prompt reply. So how do you call these constructors in the main programme. I mean how can I see the out put of e_name,e_JobTitle,e_Address?

    cheers,
    Sam
    Friday, September 30, 2005 4:04 PM
  • you dont call constructors, they get called automatically when you create an instance of Class1.

    you would create an instance like this

    Class1 myClass = new Class1('Lee','Developer','1 London Road');

    Then the private variables e_name, e_JobTitle, e_Address would contain the values you passed in.

    As for accessing these values outside the class, you would need to create public propeties thet return these values.
    Friday, September 30, 2005 4:11 PM
  • OK  keeping the following constructor in mind .

    public Class1(string name,string Title) : this(name, Title, "my default address")

    if I write

    Class1 myClass = new Class1("Lee","Developer");

    what will happen then. which constructor will be executed first and how can I see the values "Lee" and "Developer" on Console application. Any help would be greatly appreciated.

    cheers,
    Sam
    Friday, September 30, 2005 4:21 PM
  • Well there will appear two constructors to the interface the main constructor with two arguments and the overloaded constructor with three arguments.

    But there will be one implementation of the constructor and the supplied code will call the one constructor with 2 arguments.

    Therefore the two variables e_name and e_JobTitle will contain the respective values.

    then you can write

    Console.WriteLine(e_name)
    Console.WriteLine(e_JobTitle)
    Friday, September 30, 2005 4:30 PM
  • Adding on to Lee's comments, the 2-argument ctor "calls" the 3-arg ctor.

    You can also write
       Console.Writeline(e_Address);
    which will print
    "my default address"

    BTW, the "e_" prefix is unnecessary. "Preferred" would be field names like
    name, jobTitle, address, where the initial letter is lower case (so-called "camel casing").
    Friday, September 30, 2005 4:44 PM
  • Thanks for the naming conventions. But when I ran the progam below, I still found an error highlighted in green below. Could u please assist in rectifying the syntax.

     

    using System;

    using System.Collections.Generic;

    using System.Text;

    namespace Constructor_Chaining

    {

    class A

    {

    public A(string name, string Title, string Address)

    { // the "second" ctor

    e_name = name;

    e_JobTiltle = Title;

    e_Address = Address;

    }

    public A(string name, string Title)

    : this(name, Title, "my default address")

    {

    }

    }

    class B

    {

    public void Main(string[] args)

    {

    A Test = new A(Lee, Mr, "20,WER"); // Error Here says Constructor_Chaining.A.A() is inaccessible due to proctection level

    Console.WriteLine(e_name);

    Console.WriteLine(e_JobTiltle);

    }

    }

    }

     

     

    Thanks

    Subhash Subramanyam

    Sunday, March 02, 2008 12:30 PM
  • Your code looks incomplete. It doesn't have e_* members/variables defined anywhere. So, I am guessing you didnt' copy the code that you actually compiled. Anyways, try making all the class declarations as public. (However, by default class declarations should be internal and in that case too chaining should work.) I copied the same code into visual studio. Declared the e_* variables and it works. Here is the code I ran with the tweaks. (add the using lines on top).

     

    namespace Constructor_Chaining

    {

     

    class A

     

    {

    private string e_name, e_JobTiltle, e_Address;

     

    public A(string name, string Title, string Address)

    { // the "second" ctor

     

    e_name = name;

    e_JobTiltle = Title;

    e_Address = Address;

    }

     

    public A(string name, string Title)

    : this(name, Title, "my default address")

    {

    }

    }



     

    class B

    {

     

    public void Main(string[] args)

    {

     

    A Test = new A("Lee", "Mr", "20,WER"); // no error

     

    System.Console.WriteLine("e_name");

    System.Console.WriteLine("e_JobTiltle");



     

    }

    }

    }


    Anchal
    • Proposed as answer by splatBUSYsplat Tuesday, November 16, 2010 8:29 PM
    Monday, February 16, 2009 7:26 AM
  • why don't you guys use the operator ":" when you inherit the subclass

    i see you all write class b but never write b:a

    Tuesday, March 15, 2011 7:06 AM
  • Because B is not intended to inherit from A.
    Tuesday, March 15, 2011 10:11 AM
  • and, the first ctor becomes

    public Class1(string name,string Title) : this(name, Title, "my default address") 
    {}

    so wiil that "my default address" taken as the e_address?

    sushma rai
    Wednesday, August 31, 2011 9:39 AM
  • Your class is not public but the constructors are. You need to fix the protection level discrepency for it to work.
    Thursday, June 21, 2012 9:29 PM
  • Your class is not public but the constructors are.

    A public constructor can be useful whatever the accessibility of the class. It is even necessary if you want to use the class as a type parameter with a constructor constraint. The only accessibility problem in this thread is the other way: a non-public constructor.

    Wednesday, June 27, 2012 5:25 PM