C# Classes - Problem with using class as object properties

Joined
Jun 22, 2005
Messages
8
Reaction score
0
Here is my problem...

Let us say I have two classes, each representing a database table. (I won't go into methods for loading / saving . etc .. that is not my 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 person 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 be 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 that person) with the correct object. But the second method will actually 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 actually changes the object itself, instead of replacing which object I want to use)

Any suggestions???

cheers

Martiankeeper
 
Last edited:

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top