Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , ,

Sponsored Links



One constructor to call another in C Sharp

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 21-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 21-11-2009
Member
 
Join Date: May 2008
Posts: 1,990
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.
Reply With Quote
  #3  
Old 21-11-2009
opaper's Avatar
Member
 
Join Date: May 2008
Posts: 2,362
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)
__________________
The FIFA Manager 2009 PC Game

Last edited by opaper : 21-11-2009 at 02:25 AM.
Reply With Quote
  #4  
Old 21-11-2009
MindSpace's Avatar
Member
 
Join Date: Feb 2008
Posts: 1,832
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)
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "One constructor to call another in C Sharp"
Thread Thread Starter Forum Replies Last Post
Call entry stuck in Nokia C3-00 call log and stopped call logging Elijah*Blue Portable Devices 13 21-10-2011 08:49 PM
Sharp 007SH Hybrid An 3D Clamshell Android Phone by Sharp jackalz Web News & Trends 3 22-05-2011 08:28 AM
What is the use of private constructor in C++? Juaquine Software Development 5 05-03-2010 04:41 PM
copy constructor Vs constructor Truster Software Development 5 08-02-2010 02:49 PM
Array of Objects to call constructor KALLIYAN Software Development 3 04-11-2009 09:54 PM


All times are GMT +5.5. The time now is 03:34 AM.