This is a general best practice question about creating parent / child relationships with objects.
Let's say I have Wheel and Car objects and I want to Add a Wheel object to car object
public class Car{
private List<Wheel> wheels = new List<Wheel>();
void AddWheel ( Wheel WheelToAdd)
{
wheels.Add(WheelToAdd)
//Some Other logic relating to adding wheels here
}
}
}
So far so good. But what if I want to have a Car property of my wheel to say which parent car it relates to. Something like this
public class Wheel {
private Car parentCar;
public Car
{
get
{
return parentCar
}
}
}
When adding the wheel to the car, at which point would you set the parent property of the Wheel? You could set it in the Car.AddWheel method, but then the Car Property of the Wheel object would have to be Read/Write and then you could set it outside of the AddWheel method, creating an inconsistency.
Any thoughts, many thanks in advance