Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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?
0
dotnet22
Asked:
dotnet22
  • 2
  • 2
  • 2
  • +4
2 Solutions
 
praneethaCommented:
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
 
dotnet22Author Commented:
I just feel it is awkward to have a constructor with 15 values...
0
 
gregasmCommented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
gregasmCommented:
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
 
tzxie2000Commented:
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
 
praneethaCommented:
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
 
vigridCommented:
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
 
vigridCommented:
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
 
somnaticCommented:
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
 
Razzie_Commented:
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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

  • 2
  • 2
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now