Is it good programming to set instance in class without using C-tor
- From: "Tony Johansson" <johansson.andersson@xxxxxxxxx>
- Date: Wed, 06 Sep 2006 19:13:49 GMT
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
.
- Follow-Ups:
- Re: Is it good programming to set instance in class without using C-tor
- From: Bruce Wood
- Re: Is it good programming to set instance in class without using C-tor
- From: Arne Vajhøj
- Re: Is it good programming to set instance in class without using C-tor
- From: Gaurav Vaish \(www.EduJini.IN\)
- Re: Is it good programming to set instance in class without using C-tor
- Prev by Date: Re: Quick (newbie) question on C# interfaces
- Next by Date: Re: error reading excel using OleDb
- Previous by thread: access pattern
- Next by thread: Re: Is it good programming to set instance in class without using C-tor
- Index(es):