Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / November 2006

Tip: Looking for answers? Try searching our database.

Opinions Please - Properties and Constructors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John F - 20 Nov 2006 14:40 GMT
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
Miha Markic [MVP C#] - 20 Nov 2006 15:05 GMT
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).
Signature

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/

> Hello All,
>
[quoted text clipped - 12 lines]
> Thanks,
> John
Chris Dunaway - 20 Nov 2006 15:22 GMT
> Hi John,
>
[quoted text clipped - 24 lines]
> > 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.
Dave Sexton - 20 Nov 2006 15:31 GMT
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.

Signature

Dave Sexton

John F - 20 Nov 2006 16:08 GMT
Thanks for everyones opinion.  It has been very helpful!

> Hello All,
>
[quoted text clipped - 10 lines]
> Thanks,
> John
Arne Vajhøj - 23 Nov 2006 21:22 GMT
> 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
[quoted text clipped - 5 lines]
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.