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
VulpesPosted Apr 28, 2011, 6:55 AM
Ankit NandekarPosted Apr 28, 2011, 12:56 AM
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();
}
}
Mayur GujrathiPosted Apr 28, 2011, 12:39 AM
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
Sam HobbsPosted Apr 27, 2011, 11:24 PM