473,395 Members | 1,702 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,395 software developers and data experts.

Opinions Please - Properties and Constructors

Hello All,

Is it better to pass 'data' into a class object through a constructor and
then set it as a property internally in that object or is it ok to
instantiate a class object with no parameters and then attach 'data' to the
class object by merely setting a property?

The ultimate goal is to have a bunch of objects all containing say a patient
account number (the class does other things, but for sake of argument, the
data component is just patient account) set into a property for later
retrieval.

Thanks,
John
Nov 20 '06 #1
5 1416
Hi John,

If data is required at construction time then pass it through constructor.
Otherwise if number of property values isn't known, use properties or some
method which has all required arguments.
The problem with constructor is that you'll have to recreate all of them if
you decide to inherit from base class (and you want all of them).
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
"John F" <jf@rt.comwrote in message
news:19**********************************@microsof t.com...
Hello All,

Is it better to pass 'data' into a class object through a constructor and
then set it as a property internally in that object or is it ok to
instantiate a class object with no parameters and then attach 'data' to
the
class object by merely setting a property?

The ultimate goal is to have a bunch of objects all containing say a
patient
account number (the class does other things, but for sake of argument, the
data component is just patient account) set into a property for later
retrieval.

Thanks,
John
Nov 20 '06 #2
Miha Markic [MVP C#] wrote:
Hi John,

If data is required at construction time then pass it through constructor.
Otherwise if number of property values isn't known, use properties or some
method which has all required arguments.
The problem with constructor is that you'll have to recreate all of them if
you decide to inherit from base class (and you want all of them).
--
Miha Markic [MVP C#, INETA Country Leader for Slovenia]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/
"John F" <jf@rt.comwrote in message
news:19**********************************@microsof t.com...
Hello All,

Is it better to pass 'data' into a class object through a constructor and
then set it as a property internally in that object or is it ok to
instantiate a class object with no parameters and then attach 'data' to
the
class object by merely setting a property?

The ultimate goal is to have a bunch of objects all containing say a
patient
account number (the class does other things, but for sake of argument, the
data component is just patient account) set into a property for later
retrieval.

Thanks,
John

One reason to use a constructor would be if you want to create an
object but don't want to keep the variable. For example you may want
to add new objects directly to a list:

Dim ListOfObjects As New List(Of SomeClass)

ListOfObjects.Add(New SomeClass("something"))

Or perhaps a method needs to return a new instance of the class based
on parameters:

Public Function CreateAnObject(someString As String, someInteger As
Integer)

Dim something As String

'Do some calculation with the integer or some manipulation of the
string

something = <result of manipulation>

Return New SomeClass(something)

End Function

These are very simplistic examples, but using a constructor can make
the instantiating code easier to read.

In reality, what Miha wrote is correct. If the data is needed to
construct the object then use a constructor. Otherwise, it is entirely
appropriate to instantiate the object and then set the properties
later. Sometimes, you don't know what the properties should be set to
at the time of instantiation so setting the properties later is the
only option.

Nov 20 '06 #3
Hi John,
Is it better to pass 'data' into a class object through a constructor and
then set it as a property internally in that object or is it ok to
instantiate a class object with no parameters and then attach 'data' to the
class object by merely setting a property?
<snip>

That depends on whether your class is immutable or supports lazy initialization, sealed or
inheritable, and intricate or simple, and whether construction logic depends on external state.

Immutability must be enforced using constructor arguments and read-only properties. Lazy
initialization can use common constructor arguments for simplicification, but usually has the side
effect of being difficult for inheritance (as mentioned by Miha). Complex construction logic or
construction that depends on external state should probably be encapsulated using the builder
pattern.

--
Dave Sexton
Nov 20 '06 #4
Thanks for everyones opinion. It has been very helpful!

"John F" wrote:
Hello All,

Is it better to pass 'data' into a class object through a constructor and
then set it as a property internally in that object or is it ok to
instantiate a class object with no parameters and then attach 'data' to the
class object by merely setting a property?

The ultimate goal is to have a bunch of objects all containing say a patient
account number (the class does other things, but for sake of argument, the
data component is just patient account) set into a property for later
retrieval.

Thanks,
John
Nov 20 '06 #5
John F wrote:
Is it better to pass 'data' into a class object through a constructor and
then set it as a property internally in that object or is it ok to
instantiate a class object with no parameters and then attach 'data' to the
class object by merely setting a property?

The ultimate goal is to have a bunch of objects all containing say a patient
account number (the class does other things, but for sake of argument, the
data component is just patient account) set into a property for later
retrieval.
For typical data classes I would recommend having both a
no argument constructor and a constructor that sets all
the data.

It gives flexibility in the code. Often the code is
much shorter if you use a constructor with arguments
but in other cases a no argument constructor is
required.

Arne
Nov 23 '06 #6

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

Similar topics

5
by: Disiac | last post by:
Hi, MS Class Library Guidelines clearly discourages use of Write-Only properties yet they're continuously availed in .NET after VB 6. Personally, I really don't use them anywhere. Tend to find...
7
by: Alan Bashy | last post by:
Please, guys, In need help with this. It is due in the next week. Please, help me to implement the functions in this programm especially the first three constructor. I need them guys. Please, help...
3
by: john sutor | last post by:
Is it better to set a class's properties or to pass arguments to the class initially in the constructor?
9
by: Jack Addington | last post by:
I have a base form and a base logic class. Each has to know of the other. I'm then inheriting to create descendant form and descendant logic which extend both objects and again have to know of...
7
by: Mike P | last post by:
I have a class with a dozen+ properties, some of which will be set a value, and some not, depending on the constructor called. I also have a method which has only one overload and all of the...
2
by: Martin Ho | last post by:
Hi Everyone, I have this code of Mersenne twister, which produces the random numbers, one of the fastest codes as far as I know to produce random numbers. Anyways, it's writen in c# and I need to...
1
by: Corrado Labinaz | last post by:
Hi everyone, I've a Configuration class with almost 200 read only properties. This class makes available configuration data to my whole application. I would like to group realted properties...
10
by: John Swan | last post by:
Please, I have just created this site and am wondering what your opinion is from both professionals and amatures or the curious alike. Any opinions? www.integrated-dev-sol.co.uk Remove 123...
17
by: David C. Ullrich | last post by:
Having a hard time phrasing this in the form of a question... The other day I saw a thread where someone asked about overrideable properties and nobody offered the advice that properties are...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.