简体   繁体   中英

References between objects

I have been trying to figure this out for days.

What I have is three classes, let's call them City, House, Room:

class City
{
    List<House> Houses { get; set; }
    string Name { get; set; }
}

class House
{
    List<Room> Rooms { get; set; }
    string Name { get; set; }
}

class Room
{
    string Name { get; set; }
}

So, here we have a city, which holds a lot of houses. And the houses holds rooms.

Let's say for instance I get a room object, which has a name (and is grouped in the whole system with City->House->Room)

How would I be able to reference the City-objects Name variable from that Room object I have?

Like some magic way of doing "Room.House.City.Name"

I really hope you understand what I'm trying to ask, it's been driving me crazy for the last couple of days.

To make it possible you have to add additional, parent-like references into your classes:

class City
{
    List<House> Houses { get; set; }
    string Name { get; set; }
}

class House
{
    List<Room> Rooms { get; set; }
    string Name { get; set; }
    City City { get; set; }
}

class Room
{
    string Name { get; set; }
    House House { get; set; }
}

Update

Because I don't think there is a chance to have House without a City and Room without a House, I would add constructors to both House and Room classes, to bind them to parents:

class House
{
    public House(City city)
    {
        City = city;

    }
    List<Room> Rooms { get; set; }
    string Name { get; set; }
    City City { get; set; }
}

class Room
{
    public Room(House house)
    {
        House = house;
    }
    string Name { get; set; }
    House House { get; set; }
}

You can add a House property to your room and City property to your House like this:

class House
{
    List<Room> Rooms { get; set; }
    string Name { get; set; }
    public City City { get; set; }
 }

class Room
{
    string Name { get; set; }
    public House House { get; set; }
}

And when you are adding some house and room for example:

    City myCity = new City();
    House myHouse = new House { City = myCity, Name = "myHome" };
    Room myRoom = new Room { House = myHouse, Name = "myRoom" };
    myHouse.Rooms = new List<Room>();    
    myHouse.Rooms.Add(myRoom);
    myCity.Houses = new List<House>();        
    myCity.Houses.Add(myHouse);
    // here you can use:
    myRoom.House.City.Name

But this is not so elegant and it's hard to add new Houses and Rooms.Additionally I would add some methods to make it easy for example in House class:

class House 
{
     public void AddRoom(Room room)
     {
        room.House = this;
        if (Rooms == null)
            Rooms = new List<Room>();
        Rooms.Add(room);
     }
}

And then I don't need to define a Room like this:

Room myRoom = new Room { House = myHouse, Name = "myRoom" };

Instead:

myHouse.AddRoom(new Room { Name = "myRoom" });

You'll have to change your classes to contain a reference to their parent eg

class Room
{
    House House { get; set; }
    string Name { get; set; }
}

class House
{
    List<Room> Rooms { get; set; }
    string Name { get; set; }
    City City { get; set; }
}

Normally, you can't navigate from a property to it's container. This is only possible if you also have properties like House in the Room object and if you make sure that when you assign a Room the a House, you also set the House property in your Room.

However, you can use some LINQ to get the City Name, like this:

var cityWithSomeRoom = cities.Where(c = > c.Houses.Contains(h => h.Rooms.Contains(r => r.ReferenceEquals(someRoom))).FirstOrDefault();
cityWithSomeRoom.Name ; // this is your name

someRoom is the Room instance that you want to find out in which city resides. Also, make sure that you don't add the same room object to multiple houses, because ReferenceEquals will find many candidates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM