473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

Question about constructor overloading

Hello!

I am wondering what's the best practice about setting object data using
constructor parameters.

Let's say that I am creating a Person class.

class Person
{
private string name;
private Date dateOfBirth;
public Person()
{
}
public string Name
{
get {return name;}
set {name = value;}
}
public Date DateOfBirth
{
get {return dateOfBirth;}
set {dateOfBirth = value;}
}
}

Now I have options regarding how to set name and dateOfBirth.

1)

public Person(string n, Date d): this()
{
name = n;
dateOfBirth = d;
}

When using the class,

Person person = new Person(name, dob);
2)

Person person = new Person();
person.Name = name;
person.DateOfBirth = dob;
OK.
Now I want to know what's the guideline regarding the above case?
Option 1) seems to be better.
However, if there are many parameters, I need to provide all the overloading
constructors for every combination of parameters.
For example,

public Person(string n): this()
{
name = n;
}

public Person(Date d): this()
{
dateOfBirth = d;
}
Option 2) is simple.

I feel that I need some guidelines about what data should be in the
constructor's parameters.
TIA.
Sam
Nov 16 '05 #1
8 2235
Option 1 is better for the simple reason:

Person p = new Person();
Console.Write(p.Name); // BOOM!

Your worry is a false lead. You don't have to create most of those,
because you *shouldn't* create most of those. You need write just the ones
that will create a valid object. In your example, that's probably just the
one with both parameters. The best way to ensure that you never have an
invalid object, is to make it impossible to create an invalid object.

--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:e6**************@tk2msftngp13.phx.gbl...
I am wondering what's the best practice about setting object data using
constructor parameters.

Let's say that I am creating a Person class.

class Person
{
private string name;
private Date dateOfBirth;
public Person()
{
}
public string Name
{
get {return name;}
set {name = value;}
}
public Date DateOfBirth
{
get {return dateOfBirth;}
set {dateOfBirth = value;}
}
}

Now I have options regarding how to set name and dateOfBirth.

1)

public Person(string n, Date d): this()
{
name = n;
dateOfBirth = d;
}

When using the class,

Person person = new Person(name, dob);
2)

Person person = new Person();
person.Name = name;
person.DateOfBirth = dob;
OK.
Now I want to know what's the guideline regarding the above case?
Option 1) seems to be better.
However, if there are many parameters, I need to provide all the overloading constructors for every combination of parameters.
For example,

public Person(string n): this()
{
name = n;
}

public Person(Date d): this()
{
dateOfBirth = d;
}
Option 2) is simple.

I feel that I need some guidelines about what data should be in the
constructor's parameters.
TIA.
Sam

Nov 16 '05 #2
"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You need write just the ones
that will create a valid object. In your example, that's probably just the one with both parameters. The best way to ensure that you never have an
invalid object, is to make it impossible to create an invalid object.


And also, if the default constructor (no parameters) cannot create a *valid*
object (more info is necessary), then you will want to make that constructor
protected or private so that consumers cannot create an invalid one.

-- Alan

Nov 16 '05 #3
Thanks for the answer.
I have an additional question in-line.

"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Option 1 is better for the simple reason:

Person p = new Person();
Console.Write(p.Name); // BOOM!


If this is BOOM, then is a constructor Person(string name) a must, not an
option?
Date dateOfBirth is a value type and will be automatically initialize,
right?

To sum up,

name(reference type) should be in the parameter.
dateOfBirth(value type) is an optional.

Am I right?
Sam
Nov 16 '05 #4
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If this is BOOM, then is a constructor Person(string name) a must, not an
option?
I would say so.
Date dateOfBirth is a value type and will be automatically initialize,
right?


Yes... BUT... It will be initialized, but to something that is
definitely wrong. If you let it default initialize, whenever you want to
use dateOfBirth you will have to check to establish is this is a valid
birthday or a bad value. Remember, our goal here is not merely programs
which don't crash but programs that give the correct answers.
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)
Nov 16 '05 #5
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If this is BOOM, then is a constructor Person(string name) a must, not an
option?
Date dateOfBirth is a value type and will be automatically initialize,
right?

To sum up,
name(reference type) should be in the parameter.
dateOfBirth(value type) is an optional.

Am I right?


It doesn't matter if you are working with reference or value types.

What matters: If your object is not valid without the name member, then you
want to force the consumer to supply the name. If everything else is
optional, then simply provide the constructor that initializes the name, and
make the default constructor protected or private so that it cannot be
called from the outside. Initialize all of the optional values to
appropriate defaults in your constructor. Then let the consumer override
the default optional values through the properties.

-- Alan
Nov 16 '05 #6
hi
i have an opinion
when we create a class we can provide the details which uniquely
identify(primary attribures )in the cunstructor .the secondary details can be
assigned using properties.

eg in an person calss the primary details are persons identity,name ,what
ever..
the secondary details are salary details ,office phone ....

my approach is purely in a business point of view ..

regards
********
ansil
Technopark
Trivandrum
*************

"James Curran" wrote:
"Sam Sungshik Kong" <ss*@chol.nospam.net> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
If this is BOOM, then is a constructor Person(string name) a must, not an
option?


I would say so.
Date dateOfBirth is a value type and will be automatically initialize,
right?


Yes... BUT... It will be initialized, but to something that is
definitely wrong. If you let it default initialize, whenever you want to
use dateOfBirth you will have to check to establish is this is a valid
birthday or a bad value. Remember, our goal here is not merely programs
which don't crash but programs that give the correct answers.
--
Truth,
James Curran
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
(note new day job!)

Nov 16 '05 #7
Alan Pretre <al********@newsgroup.nospam> wrote:
"James Curran" <Ja*********@mvps.org> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You need write just the ones
that will create a valid object. In your example, that's probably just

the
one with both parameters. The best way to ensure that you never have an
invalid object, is to make it impossible to create an invalid object.


And also, if the default constructor (no parameters) cannot create a *valid*
object (more info is necessary), then you will want to make that constructor
protected or private so that consumers cannot create an invalid one.


No need for that unless you've got no other constructors. The default
constructor is only provided automatically if there are no other
constructors.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
No need for that unless you've got no other constructors. The default
constructor is only provided automatically if there are no other
constructors.


Thanks.

-- Alan
Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jesse | last post by:
In java, one constructor can call another constructor through this(...) for instance class foo { public: foo(int k) { this(k,false)}; foo(int k, boolean m){...}; }
2
by: Kamran | last post by:
Hi I have very little experience of C++, nevertheless I have been asked to write a gui using QT/QWT. I know that I should direct the question to the relevant mailing list and I have done that but...
7
by: Eckhard Lehmann | last post by:
Hi, I try to recall some C++ currently. Therefore I read the "Standard C++ Bible" by C. Walnum, A. Stevens and - of course there are chapters about operator overloading. Now I have a class...
26
by: Paul | last post by:
public class A { public A () { // here I would like to call the second version of _ctor, how to accomplish this ? } public A (int a, int b, int c) {
14
by: Klaus Löffelmann | last post by:
Hi, does anybody know, why the second contructor isn't called in this example? Are you able to reproduce this bug? Thanks Klaus Public Class Something
3
by: needin4mation | last post by:
The code is taken from the book Professional C#: abstract class GenericCustomer { private string name; public GenericCustomer(string name) { this.name = name; }...
3
by: iluvatar | last post by:
Hi all. I have written a 3d-vector class (for 3-dimensional space) and I have overloaded the arihtmetic operators like +, +=, * and so on. Also, the constructor works with doubles and has...
22
by: clicwar | last post by:
A simple program with operator overloading and copy constructor: #include <iostream> #include <string> using namespace std; class Vector { private: float x,y; public: Vector(float u, float...
30
by: none | last post by:
I'm trying to overload the = operator using templates, but I have some problems with one of the overloads, I would like to make something like that: intvariable = fooclass; here's a pseudo...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.