[.net] Constructors

Started by
5 comments, last by kbirger 14 years, 11 months ago
Hi, I'm creating a class in C# which contains about 10 fields. All of the fields are necessary and I would like to pass them in during construction. However, it seems silly to me to pass in circa 10 paramters into an overloaded constructor. Can someone give me some advice into handling this situation. Thanks. Thomo.
Advertisement
If you're using C# 3, you could make all of the members properties instead and use object initialisers:
class Person {	public string Name { get; set; }	public DateTime DateOfBirth { get; set; }	public string EmailAddress { get; set; }}var P = new Person() {	Name = "A. N. Other",	EmailAddress = "example@example.com",};

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

More info : http://www.programmersheaven.com/2/CSharp3-3


In case you're curious it compiles into the following code:


Person <>g__initLocal0 = new New();<>g__initLocal0.Name = "A. N. Other";Person P = <>g__initLocal0;
Quote:Original post by benryves
If you're using C# 3, you could make all of the members properties instead and use object initialisers:
*** Source Snippet Removed ***


I wouldn't recommend that in this situation. The OP has stated that all of the fields are necessary. If we follow this approach we could end up in a situation with a partially initialised object.

Using an object initialiser really works better for optional values. In this case, it is better, IMHO, to use the 10 param constructor approach.

To the OP, why does it "seem silly" to pass the 10 params to the constructor? Either they are necessary for the object or they're not. Can you provide more details as to what you're tying to achieve?
if you think programming is like sex, you probably haven't done much of either.-------------- - capn_midnight
Quote:Original post by ChaosEngine
I wouldn't recommend that in this situation. The OP has stated that all of the fields are necessary. If we follow this approach we could end up in a situation with a partially initialised object.
Agreed - it depends on how necessary said fields are. If they just need to be initialised to sensible values, I'd stick in a parameterless constructor to do so.

Of course, if all ten parameters need to be explicitly set to user-defined variables, then you'll need to pass ten parameters to a constructor.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

Yes, all of the parameters are necessary and are used later on.

I say 'seems silly' to me, since I've never had to pass that many paramters into a constructor. Granted, the design of the software should probably allow that. Unfortunately, I'm modifying a pre-existing code base and don't have much scope to change things too much.

And yes, I'm aware that I could use properties, however as some posters have stated, this is not ideal since I would end up with a half-initialised object after construction.

I've managed to separate some of the fields into new classes, since they are related in some way, and also since some of them are two "versions" of the same parameter.

Cheers.
I didn't see the comment about them being required at first. If you're passing that many parameters, perhaps some of them could take default values?

This topic is closed to new replies.

Advertisement