1

Let's say I have this 2 classes:

public class BlockBuilding
{
    private List<Room> rooms;
...

public class Room
{
    private List<Chair> chairs;
....

Then I declare an 2D array variable and assign values:

BlockBuilding[,] bB= new BlockBuilding[2,1];

Room room = new Room();
BlockBuilding b1 = new BlockBuilding();
BlockBuilding b2 = new BlockBuilding();

b1.getRooms().Add(room);
b2.getRooms().Add(room);

bB[0,0] = b1;
bB[1,0] = b2;

Chair c1 = new Chair();
Chair c2 = new Chair();

bB[0,0].getRooms()[0].getChairs().Add(c1);
bB[1,0].getRooms()[0].getChairs().Add(c2);

Assuming all the necessary data is there and there is no NullException issue, when I tried to display the chairs in bB[0,0] and bB[1,0], surprisingly they both contained c1 and c2 while each of them should only contain one chair.

All above is similar code structure to my codes and no disclosure of my codes is allowed due to disclosure agreement restrictions. I have been haunted by this bug for weeks. Any help would be greatly appreciated!

Jeff
  • 293
  • 4
  • 13
  • 2
    You have a single room. Why wouldn't it contain both chairs then? – Sami Kuhmonen Oct 11 '16 at 17:28
  • 5
    You added the same room to both buildings. – juunas Oct 11 '16 at 17:28
  • I supposed the room is added by value instead of by reference. I will try to solve the issue from this direction then. – Jeff Oct 11 '16 at 17:30
  • You create one room, `room` and you add it to two building `b1` and `b2`. It's not being "added by value", you are adding the a reference to *the same object* to both buildings. You just need to instantiate two separate rooms and add them to their respective buildings. Just like how you created two chairs – Matt Burland Oct 11 '16 at 17:32

3 Answers3

0

you should use two diferent rooms, because since it is a reference, you are adding the same room for both BlockBuildings...

Room room1 = new Room();
Room room2 = new Room();
BlockBuilding b1 = new BlockBuilding();
BlockBuilding b2 = new BlockBuilding();

b1.getRooms().Add(room1);
b2.getRooms().Add(room2);
lem2802
  • 1,152
  • 7
  • 18
  • Instead of making room variables create the room in the add method. – deathismyfriend Oct 11 '16 at 17:42
  • yes, it is an other way, but for new devs is something difficult to understand :) – lem2802 Oct 11 '16 at 17:44
  • @deathismyfriend: With the code the OP has, I think it would actually be easier to create the `room1` and `room2` variables and assign the chairs to them and avoid the whole `bB[0,0].getRooms()[0].getChairs().Add(c1);` part which looks really ugly. – Matt Burland Oct 11 '16 at 18:00
0

class is passed by reference, so you added the same object to both rooms.

If you want "copy by value" functionality you either need to use struct (make sure first to read some of "C# value vs. reference types" articles like References and Values).

Alternatively you can make deep copy (Deep cloning objects) of an object before adding to the list.

Community
  • 1
  • 1
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

Thanks guys! Indeed I am new to C# and my project is heavily relying on it. There were too much useful answer and I can't actually rate anyone as "most" useful answer as all of you guys are the best! And yes the issue was with the single Room instance, create distinct Room instance for each of the BlockBuilding would solve the problem right away.

Jeff
  • 293
  • 4
  • 13