Solved

what to put in the constructor...

Posted on 2004-09-20
10
216 Views
Last Modified: 2010-04-15
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?
0
Comment
Question by:dotnet22
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 15

Expert Comment

by:praneetha
ID: 12105984
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
0
 

Author Comment

by:dotnet22
ID: 12106121
I just feel it is awkward to have a constructor with 15 values...
0
 
LVL 8

Expert Comment

by:gregasm
ID: 12106137
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.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 8

Expert Comment

by:gregasm
ID: 12106157
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
}
0
 
LVL 5

Expert Comment

by:tzxie2000
ID: 12106176
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
0
 
LVL 15

Expert Comment

by:praneetha
ID: 12106235
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
0
 
LVL 4

Accepted Solution

by:
vigrid earned 350 total points
ID: 12106888
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;
  }
}
0
 
LVL 4

Expert Comment

by:vigrid
ID: 12106904
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.

HTH
0
 
LVL 4

Expert Comment

by:somnatic
ID: 12109971
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.
0
 
LVL 8

Assisted Solution

by:Razzie_
Razzie_ earned 150 total points
ID: 12111582
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";
SmtpMail.Send(mMsg);

That is much better than MailMessage mMsg = new MailMessage("from",  "to", "body", "Cc", "Bcc", MailMessage.Priorty.PriorityHigh, ... ,.... ,... ) - you get the idea :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question