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

Constructors or properties

Is it better to set a class's properties or to pass arguments to the class
initially in the constructor?
Nov 15 '05 #1
3 1731
john sutor <jo********@cinfin.com> wrote:
Is it better to set a class's properties or to pass arguments to the class
initially in the constructor?


It depends - there are certainly times when it's nice to have a
parameterless constructor, but at other times it's often worth making
sure that by the time the constructor returns, the object is in a valid
state - and that will often mean you *have* to give parameters.

Also, if you give the parameters in the constructor you may be able to
make the object immutable (ie nothing changes its state) which is handy
for multithreading, parameter passing etc.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
It depends on what you want to do. If you want to set the value once and not
try to set it again use the constructor. But if you want to change the value
of the field more than once, make the property settable.

HTH,
Martha
"john sutor" <jo********@cinfin.com> wrote in message
news:eg**************@TK2MSFTNGP10.phx.gbl...
Is it better to set a class's properties or to pass arguments to the class
initially in the constructor?

Nov 15 '05 #3
> Is it better to set a class's properties or to pass arguments to the class
initially in the constructor?


The two concepts are not related. You pass arguments to the constructor when
you want to use that data to initialize your object. You set properties when
you want to change the current value of the property. One is not better than
the other.

Another thing constructors are good for is letting you know what the
*minimum* information for constructing an object is. For example, look at
this class:

class Person {
private string name;
private int age;

public Person(string name) {
this.name = name;
}

public Person(string name, int age) {
this.name = name;
this.age = age;
}

public string Name {
get {
return this.name;
}
}

public int Age {

get {
return this.age;
}

set {
this.age = value;
}
}
}

You can choose to construct a new Person passing both the name and age as
arguments to the constructor, or just the name. But in all cases you *must*
provide the name. You can set the age later, when you have the data
available or when it changes.

I hope that clears it up,
-JG
Nov 15 '05 #4

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

Similar topics

1
by: andrea_gavana | last post by:
Hello NG, I am trying to port a useful class from wxWidgets (C++) to a pure Python/wxPython implementation. In the C++ source code, a unique class is initialized with 2 different methods (???)....
42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
1
by: Daniel Klein | last post by:
When creating a custom exception that derives from ApplicationException, why is it necessary to have the 3 basic contructors, i.e. Public Sub New() Public Sub New(ByVal message As String)...
10
by: Kevin Buchan | last post by:
I searched the news group and could not find an answer to this question, so I'll go ahead and post it. Let's say I have a class A with a couple different constructors... nothin' special. Now,...
10
by: John | last post by:
Trying to find out what is essential / optional, I made an extremely simple Class and Module combination to add two numbers. (see below) It appears that an empty constructor is needed n order to...
22
by: Peter Morris [Droopy eyes software] | last post by:
Look at these two classes public class Test { public readonly string Name; public Test(string name)
7
by: andrewfsears | last post by:
I have a question: I was wondering if it is possible to simulate the multiple constructors, like in Java (yes, I know that the languages are completely different)? Let's say that I have a class...
6
by: raylopez99 | last post by:
This thread is about how variables or parameters (including objects) are passed between forms (namely, using parameterized constructors, e.g., to pass an int between forms (e.g., a calling form and...
13
by: =?Utf-8?B?QW5kcmVhcw==?= | last post by:
Hi, I would like to get some thoughts on Overloaded constructors vs. Object initializations. Assuming that the class supports a default constructor, is there any reason to include overloaded...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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...

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.