what to put in the constructor...

I have a design question. Suppose you have a class that has 15 properties... Is it smart to require a user to pass in all values when creating an object? What should go in to the constructor parameter list? Are there any guidelines to follow when creating an object?
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

you can make it require user to pass values..

just remove the default constructor and add a constructor which takes 15 parameters and then inside the constructor set the parameter values.....

that should solve your problem..or you can have a constructor with out parameters ...but inside the constructor just set the parameter values to what ever u want....

u can both 2...if user does not pass the parameters ..just intitalize with your default data...

if he does just initialize with the user data.

Thank you
dotnet22Author Commented:
I just feel it is awkward to have a constructor with 15 values...
In some classes, you might need only some of the members initialized, and in other classes, you might need none or all of the members initialized.

So, in the constructor, you take the requirements into account and add those parameters to intialize the required members.

For the ultimate in flexibility, you can OVERLOAD THE CONSTRUCTOR to allow different initialization methods.
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

string _one;
string _two;

public Myclass(string one, string two)
//in this constructor, initialize both with user values
_one = one;
_two = two;

public Myclass(string one)
//in this constructor, init one with user val, init two with default
_one = one;
_two = ""; //initialize this one to a default
I think it is not a good idea to write a constructor with 15 params
first you can assign default values to the property in constructor,normally the class doing as this
like TextBox and.... the component in .Net always doing in this way

second I think it may be a little strange that a class with so many properties. My mean is not it is a unpossible class,but you may review the class and considered whether the class can split to some little class,and you can compose or inherited this classes to the final class you used. this will be a scalable class design
then just use parameterless constructor and initialize to teh values u want too...

and user can set the propertied if they want any property to be initializd to their own choice...

good luck
Good solution to many constructors for one class is to have an Init() method.

class MyClass
  private int myInt1;
  private int myInt2;
  private int myInt3;
  private string myString1;
  private string myString2;
  private string myString3;

  public MyClass()
    Init(1, 2, 3, "a", "b", "c");

  public MyClass(int my1, int my2, int my3)
    Init(my1, my2, my3, "a", "b", "c");

  public MyClass(string my1, string my2, string my3)
    Init(1, 2, 3, my1, my2, my3);

  public MyClass(int my1, int my2, int my3, string my4, string my5, string my6)
    Init(my1, my2, my3, my4, my5, my6);

  private void Init(int my1, int my2, int my3, string my4, string my5, string my6)
    myInt1 = my1;
    myInt2 = my2;
    myInt3 = my3;
    myString1 = my4;
    myString2 = my5;
    myString3 = my6;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
That way you can join flexibility of multiple overrides with simplicity of having one constructor implementation. And it's up to you to choose what do you want to get as an argument for the constructor. Basically, having methods or constructors with more than 7-8 arguments is making your code too hard to read. Consider creating a struct that you will pass instead of passing multiple simple values.

Do not do that ..

Microsoft has done it and all sites like codeproject are complaining. They did it for interop with excel/word and those products.

As stated above, write constructors with a smaller number of params (absolute maximum: 6) and provide default values which seem to be logical to the user ...

Only put the things into constructor which HAVE to be determined by the user.

If your program needs ALL the params (would know why), provide an empty constructor and document that the user should use the poperties for setting values ...

But if possible, give reasonable results by yourself.
It really depends on the absolutely necessary values. Optional values should not be put in the constructor when you have as much as 15 of them.

Take, for a good example, the MailMessage class. It takes no parameters in the constructor at all, while it has 14 properties. All of them are assigned after initializing:

MailMessage mMsg = new MailMessage();
mMsg.From = "from";
mMsg.To = "to";
mMsg.Body = "body";

That is much better than MailMessage mMsg = new MailMessage("from",  "to", "body", "Cc", "Bcc", MailMessage.Priorty.PriorityHigh, ... ,.... ,... ) - you get the idea :)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.