Hello!
I am wondering what's the best practice about setting object data using
constructor parameters.
Let's say that I am creating a Person class.
class Person
{
private string name;
private Date dateOfBirth;
public Person()
{
}
public string Name
{
get {return name;}
set {name = value;}
}
public Date DateOfBirth
{
get {return dateOfBirth;}
set {dateOfBirth = value;}
}
}
Now I have options regarding how to set name and dateOfBirth.
1)
public Person(string n, Date d): this()
{
name = n;
dateOfBirth = d;
}
When using the class,
Person person = new Person(name, dob);
2)
Person person = new Person();
person.Name = name;
person.DateOfBirth = dob;
OK.
Now I want to know what's the guideline regarding the above case?
Option 1) seems to be better.
However, if there are many parameters, I need to provide all the overloading
constructors for every combination of parameters.
For example,
public Person(string n): this()
{
name = n;
}
public Person(Date d): this()
{
dateOfBirth = d;
}
Option 2) is simple.
I feel that I need some guidelines about what data should be in the
constructor's parameters.
TIA.
Sam