C# Classes - Problem with using class as object properties

C# Classes - Problem with using class as object properties

Post by Martiankee » Sat, 25 Jun 2005 23:41:42



Here is my problem...

Let us say I have two classes, each representing a database table. (
won't go into methods for loading / saving . etc .. that is not m
problem)


Code
-------------------
class HairColourClass
{
int ID;
string Colour;
}

class PersonClass
{
int ID;
string FName;
string SName;
HairColourClass HColour;
}

-------------------


My problem is this ... I can add or change the haircolour of a perso
object quite easily.


Code
-------------------
// nothing wrong with this
PersonClass newPerson = new PersonClass;
newPerson.ID = 0
newPerson.FName = "John";
newPerson.SName = "Smith";
newPerson.HColour = HairColourClass.GetColour("Blonde");
//returns the object I want, with the correct ID and Colour properties.
newPerson.Save()
-------------------


BUT .. now the person dyes their hair and the value needs to b
changed
(and here is my problem) ..


Code
-------------------

PersonClass newPerson = PersonClass.GetPersonFromID(0);

// CORRECT METHOD
newPerson.HColour = HairColourClass.GetColour("Brown");

// WRONG !! this changes the lookup table, which screws all my links!!!
newPerson.HColour.Colour = "Brown";

-------------------


I hope you understand my problem.

I want to replace the HColour object (which is the property of tha
person) with the correct object. But the second method will actuall
change what that object's properties are (changing it in the database)

in other words ...
I want the developer to use <objectA>.<objectB> = <NewobjectB>
and NOT .. <objectA>.<objectB>.<property> = <value> (which actuall
changes the object itself, instead of replacing which object I want t
use)

Any suggestions???

cheers

Martiankeepe
 
 
 

C# Classes - Problem with using class as object properties

Post by Jon Skeet » Wed, 29 Jun 2005 04:02:39


<snip>


Yup - where possible, make the objects immutable as far as the outside
world concerned. It's not always easy, but if you're running into
problems you can often get away with a wrapper class (which is
immutable) which just proxies calls onto your mutable data class, and
you make sure that the mutable version which is wrapped never "leaks"
to the outside world.

--
Jon Skeet - < XXXX@XXXXX.COM >
http://www.yqcomputer.com/ ~skeet
If replying to the group, please do not mail me too

 
 
 

C# Classes - Problem with using class as object properties

Post by Martiankee » Wed, 29 Jun 2005 16:42:38


So .. basically ... (just to get this straight)

your suggestion is .. make what is effectively a blank "read only
class which inherits from my base class, and overrides the Se
statements for my properties, and also overrides the Save( ) method fo
my classes.

So for each class which I want to use as a property (or read-onl
object in a collection) I should create 2 classes.

1x Base class, fully functional
1x Read-Only wrapper class

Is this what you mean?

thanks very much for your suggestions

Martiankeepe
 
 
 

C# Classes - Problem with using class as object properties

Post by Jon Skeet » Thu, 30 Jun 2005 01:26:22


No, I wouldn't do it through inheritance. I'd either:

a) Have a read-only flag in the class, so you can "freeze" a specific
instance

or

b) Have a separate class which aggregates the original one, and use an
interface to expose it

If you *really* wanted to do it with inheritance, do it the other way
round - have protected setters in the base class, and public setters in
the derived class, and a constructor in the base class which
initialises itself to have the same contents as a parameter which is
passed in.

--
Jon Skeet - < XXXX@XXXXX.COM >
http://www.yqcomputer.com/ ~skeet
If replying to the group, please do not mail me too