Results 1 to 4 of 4

Thread: One constructor to call another in C Sharp

  1. #1
    Join Date
    Nov 2009
    Posts
    67

    One constructor to call another in C Sharp

    I have begin to learn C sharp since last month. I have studied HTML and MS-SQL earlier but nothing as such in software programming language. For this reason I am finding learning C sharp very difficult. I have a doubt in Constructor topic. My doubt is can one constructor call another? Please illustrate with an example if possible.

  2. #2
    Join Date
    May 2008
    Posts
    2,012

    Re: One constructor to call another in C Sharp

    The solution to your doubt is that, Yes in C-Sharp one constructor can call another. The following code will show how to call one constructor from another constructor.
    Code:
    public class Top : Stud
    {
       public Top (int holiday) : base (holiday)
       {
         .
         .
         . 
       }
    }
    This code makes use of base keyword before the body of the constructor. This is used to call a C-Sharp constructor in the parent class. It is very important to first get the base clear for the constructor topic before getting in to detail part of. C-Sharp is a programming language that will help you to develop applications for .NET framework.
    Last edited by Katty; 21-11-2009 at 02:36 AM.

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: One constructor to call another in C Sharp

    Code:
    public class mySampleClass
     {
       public Test(): this(10)
    	{
                          // parameterless constructor
    	}
       public Test(int Age)
    	{
                        // constructor with single parameter
    	}
    }
    In the above code the constructor with a single parameter is called by the consructor without any parameter. It is difficult to grasp C# if you have never studied or have no idea of Java and C++. Before getting your hands on C# you must have the knowledge of C++ and Java. In C# t is possible to call one constructor from inside the other constructor. The basic syntax is to use the this keyword,
    Code:
    : this (parameters)
    Last edited by opaper; 21-11-2009 at 02:25 AM.

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: One constructor to call another in C Sharp

    Here is a very simple example to illustrate how one constructor can call another in Csharp programming language. Let us assume the following code see where we have to call the seond constructor,
    Code:
    public class Abc
    {
    public Abc ()
    {
    // to call the second constructor here
    }
    
    public Abc (int n1, int n2, int n3)
    {
    // some code
    }
    }
    The solution is to use:
    Code:
    public Abc (int param1, int param2, int param3) : base(param1, param2, param3)

Similar Threads

  1. Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp
    By jackalz in forum Web News & Trends
    Replies: 3
    Last Post: 22-05-2011, 07:28 AM
  2. What is constructor overloading in c++
    By Mast Maula in forum Software Development
    Replies: 4
    Last Post: 08-01-2011, 10:34 AM
  3. copy constructor Vs constructor
    By Truster in forum Software Development
    Replies: 5
    Last Post: 08-02-2010, 02:49 PM
  4. Selecting a constructor to use with new in C++
    By Carlton in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:30 AM
  5. Array of Objects to call constructor
    By KALLIYAN in forum Software Development
    Replies: 3
    Last Post: 04-11-2009, 09:54 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,494,891.01156 seconds with 16 queries