You can use the automatically implemented property:
public class Cupboard { public Room Parent { get; set; } }
You can also make a private setter and install it in the constructor.
public class Cupboard { public Cupboard(Room parent) { this.Parent = parent; } public Room Parent { get; private set; } }
Using:
Room room = new Room(); Cupboard cupboard = new Cupboard(room); Console.WriteLine(cupboard.Parent.ToString());
If you have many objects that have a parent room, you can create an interface so that you can find out which room is the parent of the object without knowing its specific type.
interface IRoomObject { Room { get; } } public class Cupboard : IRoomObject {
source share