Hi, How can I call multiple constructors for the following :
class sample
{
string str_a;
int int_b;
sample()
{
/*Constructor - 1*/
}
sample(string a) : this()
{
/*Constructor - 2*/
str_a = a;
}
sample(int b) : this()
{
/*Constructor - 3*/
int_b = b;
}
sample(string a, int b) : this()
{
/*4th Constructor*/
}
}
How can I call the 1,2,3 constructors in 4th constructor in easy way.
Loading

Sam HobbsRank #107 · 20.5K reputationPosted Apr 27, 2011, 7:24 PM EDT
Mayur GujrathiRank #666 · 3.6K reputationPosted Apr 27, 2011, 8:39 PM EDT
in this way
sample obj=new sample();//call to your default constructor
sample obj1=new sample("some string");//call to your second constructor
sample obj2=new sample(10);//call to your third constructor
sample obj3=new sample(10,"some string");//call to your fourth constructor
for more refer my article
http://www.c-sharpcorner.com/UploadFile/5eae74/6996/?ArticleID=7fb8bfa0-e538-4665-8c7f-0bfeb42e4455
Ankit NandekarRank #703 · 3.4K reputationPosted Apr 27, 2011, 8:56 PM EDT
class sample
{
string str_a;
int int_b;
sample():this("ankit")
{
Console.WriteLine("1");
/*Constructor - 1*/
}
sample(string a) : this(1)
{
/*Constructor - 2*/
str_a = a;
Console.WriteLine("2");
}
sample(int b) : this("ankit",2)
{
/*Constructor - 3*/
int_b = b;
Console.WriteLine("3");
}
sample(string a, int b)
{
/*4th Constructor*/
Console.WriteLine("4");
}
static void Main(string[] args)
{
sample obj = new sample();
Console.ReadLine();
}
}
VulpesRank #55 · 29.2K reputationPosted Apr 28, 2011, 2:55 AM EDT