neildt
neildt

Reputation: 5352

C# class with array of classes

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 ?

Upvotes: 1

Views: 2559

Answers (3)

Jodrell
Jodrell

Reputation: 35716

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.

Upvotes: 0

S2S2
S2S2

Reputation: 8502

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"); 

Upvotes: 0

CodeCaster
CodeCaster

Reputation: 151586

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);

Upvotes: 4

Related Questions