J
Joe Kasta
Hi,
I'm trying to determine what the proper way to create objects would be in C#
(or any language, practically).
There are two different ways of implementing objects that I can see, and I
hope someone can give their two cents as to which is more preferred.
Object 1: Have a Class with readonly properties and mutate all of the
private variables directly within the class. Any function within the class
may directly modify any private variable within the class.
Object 2: Have a Class with both sets of properties and only access the
Private Variables using those properties, Including directly within the
class (The Property itself, of course can mutate / access the variable, but
nothing else.)
I'm torn. I can see value in both.
In Object 1: It saves problems and you prevent any possible manipulation of
the properties from outside sources. (You can have a readonly Get property,
and any holding a reference the Object 1 can't manipulate the private
variable at all. This is good, right?!
In Object 2: You risk having outside objects manipulating the private
variables. BUT, in inherited classes, you can change practically anything
and not have to worry about changing a base class if you need functionality.
Any thoughts on this, people?
-J
I'm trying to determine what the proper way to create objects would be in C#
(or any language, practically).
There are two different ways of implementing objects that I can see, and I
hope someone can give their two cents as to which is more preferred.
Object 1: Have a Class with readonly properties and mutate all of the
private variables directly within the class. Any function within the class
may directly modify any private variable within the class.
Object 2: Have a Class with both sets of properties and only access the
Private Variables using those properties, Including directly within the
class (The Property itself, of course can mutate / access the variable, but
nothing else.)
I'm torn. I can see value in both.
In Object 1: It saves problems and you prevent any possible manipulation of
the properties from outside sources. (You can have a readonly Get property,
and any holding a reference the Object 1 can't manipulate the private
variable at all. This is good, right?!
In Object 2: You risk having outside objects manipulating the private
variables. BUT, in inherited classes, you can change practically anything
and not have to worry about changing a base class if you need functionality.
Any thoughts on this, people?
-J