473,396 Members | 1,834 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.

Is it good programming to set instance in class without using C-tor

Hello!

I have a class definition called MyClass see below.

I create an instance of this class MyClass
I also want this instance to be able to modify the test instance that exist
in this class.

I can make the instance of this class MyClass to be able to access the
instance test in two ways.

I can either pass an instance of this class MyClass in the c-tor and assign
the instance to the test field.

I can also use a set property by calling the property. Here I set an
instance to the test field.
public MyClass Test
{
set {myTest= value;}
}

Now to my question is it bad programming to use the second method which was
to use a property to set the instance test in the object.

A disadvantage to use a C-tor is if I want to add another argument to a
C-tor and this C-tor is called in many different forms I have to change in
many places.

If I instead use the property to set the test instance in the class it is
sufficient to add just a call to the setter property.

public class MyClass
{
private MyClass test;

public MyClass(...)
{

}

public MyClass Test
{
set {myTest= value;}
}
}

//Tony

Sep 6 '06 #1
4 1527
Hi,
Now to my question is it bad programming to use the second method which
was to use a property to set the instance test in the object.
Depends on your requirement.
If you want to you use MyClass in such a way that the instance (in
private field) is not alterable, don't publish a write-property. Use it in
constructor.
A disadvantage to use a C-tor is if I want to add another argument to a
C-tor and this C-tor is called in many different forms I have to change in
many places.
There's no case of good-or-bad programming here.
It's just the case of use.

For example, if you look into the various *EventArgs classes, you'd find
the members being settable only through ctor and not as properties.

However, classes would allow you to set the properties directly... it's
just the scenario.
--
Happy Hacking,
Gaurav Vaish | http://www.mastergaurav.com
http://articles.edujinionline.com/webservices
-------------------
Sep 6 '06 #2
Tony Johansson wrote:
I have a class definition called MyClass see below.

I create an instance of this class MyClass
I also want this instance to be able to modify the test instance that exist
in this class.

I can make the instance of this class MyClass to be able to access the
instance test in two ways.

I can either pass an instance of this class MyClass in the c-tor and assign
the instance to the test field.

I can also use a set property by calling the property. Here I set an
instance to the test field.
In general I prefer both:
- no arg constructor + property setter
- constructor with args

But in your specific case (where the property is the same
type as the class itself) I think you can not pass
the arg in the constructor or you will get an infinite
source code (unless you have another way of creating the
test object).

Arne
Sep 6 '06 #3

Arne Vajhøj wrote:
Tony Johansson wrote:
I have a class definition called MyClass see below.

I create an instance of this class MyClass
I also want this instance to be able to modify the test instance that exist
in this class.

I can make the instance of this class MyClass to be able to access the
instance test in two ways.

I can either pass an instance of this class MyClass in the c-tor and assign
the instance to the test field.

I can also use a set property by calling the property. Here I set an
instance to the test field.

In general I prefer both:
- no arg constructor + property setter
- constructor with args

But in your specific case (where the property is the same
type as the class itself) I think you can not pass
the arg in the constructor or you will get an infinite
source code (unless you have another way of creating the
test object).
....or you allow the argument to be null.

Sep 6 '06 #4

Tony Johansson wrote:
Hello!

I have a class definition called MyClass see below.

I create an instance of this class MyClass
I also want this instance to be able to modify the test instance that exist
in this class.

I can make the instance of this class MyClass to be able to access the
instance test in two ways.

I can either pass an instance of this class MyClass in the c-tor and assign
the instance to the test field.

I can also use a set property by calling the property. Here I set an
instance to the test field.
public MyClass Test
{
set {myTest= value;}
}

Now to my question is it bad programming to use the second method which was
to use a property to set the instance test in the object.

A disadvantage to use a C-tor is if I want to add another argument to a
C-tor and this C-tor is called in many different forms I have to change in
many places.

If I instead use the property to set the test instance in the class it is
sufficient to add just a call to the setter property.

public class MyClass
{
private MyClass test;

public MyClass(...)
{

}

public MyClass Test
{
set {myTest= value;}
}
}

//Tony
For me, the question of what arguments should go on the constructor is
really a different question: what constitutes a "correctly formed
instance" of your object? By that I mean, what is the minimum amount of
information that must be set in your object in order that it be usable
by a client application? Then, within that set, which ones have
defaults? Any object property that is required in order that the object
be usable and that has no default must be passed on the constructor.
Either that or you implement hokey schemes like "Valid" properties that
your client has to test before they try to use an object instance.

If every MyClass object has to have a Test object, or the original
MyClass object is useless, and there is no such thing as a default Test
object, then put it on the constructor. This means, "If you don't pass
me one of these then I can't function properly." If the Test object is
optional, or if you can provide a default Test object if one isn't
supplied, then make it a settable property and leave it off the minimal
constructor.

You might have it on the constructor _and_ a property if it's required
for the proper functioning of every MyClass instance, but it's also
changeable by the client.

Oh, yeah. That's another consideration: can the client reasonably
change this Test object during the execution of a program? If not, even
if it's not required, then it should be on a constructor, even if it's
optional (there are other constructors that don't require it). If so,
then you should provide a settable property, and it may or may not be a
parameter to a constructor (for convenience).

(Of course, as another poster pointed out, if you have a circular class
reference like this, there must be a way to create an instance without
supplying a Test object, or you could never create the first one. This
way of creating a Test instance, though, does not need to be directly
accessible to client programs.)

Sep 6 '06 #5

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

Similar topics

24
by: matty | last post by:
Go away for a few days and you miss it all... A few opinions... Programming is a craft more than an art (software engineering, not black magic) and as such, is about writing code that works,...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
28
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
7
by: tada991 | last post by:
Hello Everyone, I just purchased Visual Studio .Net Architect 2003 and want to know what's a good book for begginers to start with. I know nothing about programming whatsoever, but I do have a...
3
by: Sai Kit Tong | last post by:
I posted for help on legacy code interface 2 days ago. Probably I didn't make it clear in my original mail. I got a couple of answers but none of them address my issues directly (See attached...
13
by: KV | last post by:
I'm new to OO Design, and I'm fixing to start writing my very first C# program. Given the complexity of OO programming, I would like to run something by this group and get general input. My...
45
by: Brett | last post by:
If I do this without declaring a corresponding field, is it considered bad design? What are the advantages or disadvantages to either method? Notice there is not set. public string URL { get...
150
by: tony | last post by:
If you have any PHP scripts which will not work in the current releases due to breaks in backwards compatibility then take a look at http://www.tonymarston.net/php-mysql/bc-is-everything.html and...
206
by: WaterWalk | last post by:
I've just read an article "Building Robust System" by Gerald Jay Sussman. The article is here: http://swiss.csail.mit.edu/classes/symbolic/spring07/readings/robust-systems.pdf In it there is a...
23
by: tonytech08 | last post by:
What I like about the C++ object model: that the data portion of the class IS the object (dereferencing an object gets you the data of a POD object). What I don't like about the C++ object...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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.