1

I've the following class;

public class Hotel
{
    public int Id { get; set; }
    public Room[] room { get; set; }

    public class Room
    {    
        public int RoomId { get; set; }
        public int RoomTypeId { get; set; }

        public string Name { get; set; }        
    }
}

I can create a instance of the class like below as assign data fine

Hotel oHotel = null;
oHotel = new Hotel ();
oHotel.Id = 100;

But how do I create a sub instance for the Room class which I need to add associated data for the Hotel class ?

3
  • As a note: you should create a separate .cs file for each class. Commented Jun 21, 2013 at 11:08
  • Have a search up of inheritance Commented Jun 21, 2013 at 11:11
  • try Hotel.Room room = new Hotel.Room(); Commented Jun 21, 2013 at 11:19

3 Answers 3

4

By giving the room a reference back to the hotel in its constructor. But then the hotel doesn't know about the room, so add a method for that too:

public class Hotel
{
    public int Id { get; set; }
    public List<Room> Rooms { get; set; }

    public void AddRoom(Room room)
    {
        Rooms.Add(room);
    }
}

public class Room
{    
    public Hotel Hotel { get; private set; }
    public int RoomId { get; set; }
    public int RoomTypeId { get; set; }

    public string Name { get; set; }        

    public Room(Hotel hotel)
    {
        this.Hotel = hotel;
    }
}

Then you can just call:

var hotel = new Hotel();

var room = new Room(hotel);

hotel.AddRoom(room);
Sign up to request clarification or add additional context in comments.

6 Comments

I tried the example above, but when calling Rooms.Add(room); I get an error "Object reference not set to an instance of an object." Any ideas why ?
Because hotel.Rooms is null. Initialize it to an empty list in the Hotel constructor.
Rooms = new List<Room>();.
It is kind of working how I envisaged. However, I'm getting a copy of the Class Hotel under each Room ?
By removing public Room(Hotel hotel) it has fixed the duplication of data
|
0
public class Hotel
{
    public int Id { get; set; }
    public Room[] Rooms { get; set; }

    public void AddRoom(int id, int typeID, string name)
    {
        Room room = new Room(id, typeID, name);
        this.Rooms.Add(room);
    }

    public class Room
    {    
        public int RoomId { get; set; }
        public int RoomTypeId { get; set; }

        public string Name { get; set; }        

        public Room(int id, int typeID, string name)
        {
            RoomID = id;
            RoomTypeId = typeID;
            Name = name;
        }
    }
}

Client Code:

Hotel oHotel = null;
oHotel = new Hotel ();
oHotel.Id = 100;

oHotel.AddRoom(1, 1, "Name1"); 

Comments

0

With a combination of array and object initializer syntax you can do it all in one statement, this is entirely appropriate for instantiating POCOs.

var oHotel = new Hotel
    {
        Id = 100
        room =
            {
                new Hotel.Room { RoomId = 1, RoomTypeid = 1, Name = "Name" }
            }
    };

If want to initialize your hotel with mith multiple rooms,

var oHotel = new Hotel
    {
        Id = 100
        room =
            {
                new Hotel.Room { RoomId = 1, RoomTypeid = 1, Name = "A" },
                new Hotel.Room { RoomId = 2, RoomTypeid = 1, Name = "B" },
                new Hotel.Room { RoomId = 3, RoomTypeid = 2, Name = "C" },
                new Hotel.Room { RoomId = 4, RoomTypeid = 2, Name = "D" }
            }
    };

As an aside, your

public Room[] room { get; set; }

property, should probbably be called Rooms.


If you don't wan't to use POCOs, like you show in you question, I'd rewrite your class like this, making it immutable,

public class Hotel
{
    private readonly int id;
    private readonly IList<Room> rooms;

    public Hotel(int id; IEnumerable<Room> rooms)
    {
        this.id = id;
        this.rooms = rooms.ToList();
    }

    public int Id
    {
        get { return this.id; }
    }

    public IEnumerable<Room> Rooms
    {
        get { return this.rooms; }
    }

    public class Room
    {
        private readonly int id;
        private readonly RoomType type;
        private readonly string name;

        public Room(int id, RoomType type, string name)
        {

        }

        public int Id
        { 
           get { return this.id; }
        }

        public RoomType Type
        {
            get { return this.type; }
        }

        public string Name
        {
           get { return this.name; }
        }
    }

    public enum RoomType
    {
        // Valid Room Types Here,
        // ...
    }
}

then I'd instantiate it like this,

var oHotel = new Hotel(
    100,
    {
        new Hotel.Room(1, Hotel.RoomType..., "A"),
        new Hotel.Room(2, Hotel.RoomType..., "B"),
        new Hotel.Room(3, Hotel.RoomType..., "C"),
        new Hotel.Room(4, Hotel.RoomType..., "D") 
    });

still in a single statement but, more compact. The resultant object would be immutable, this has numerous benefits beyond the scope of the question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.