Solved

what to put in the constructor...

Posted on 2004-09-20
10
213 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
Comment Utility
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
Comment Utility
I just feel it is awkward to have a constructor with 15 values...
0
 
LVL 8

Expert Comment

by:gregasm
Comment Utility
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
 
LVL 8

Expert Comment

by:gregasm
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 15

Expert Comment

by:praneetha
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
.xltm file opens as .xlsx file 3 36
Duplicate a row 2 29
Refresh Datagridview from another form 1 25
Exception in Log4Net 1 17
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
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…
This video discusses moving either the default database or any database to a new volume.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now