Jump to content
  • Advertisement
Sign in to follow this  
Thomo

[.net] Constructors

This topic is 3321 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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",
};

Share this post


Link to post
Share on other sites
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;



Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!