T
Tony Johansson
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
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