Click here to Skip to main content
15,900,816 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everyone,
I am a freshman in C#, and stuck with a logical/conceptual problem in my hobby project. I want a good object oriented approach, but can not figure out how :confused:

I've simplified my problem for easy understanding.
Consider, a class "Home", it has many different kind of Rooms. Each room share some common property and has many different properties.
Please read the code, its self explainatory.

Ques A : How to get all the rooms's appropriate data in "writeAllRoomData" function ?

Ques B : Is there a better way, so that each instance of Room(of a particular roomtype) does not have the burden of other roomtype's vars?
(e.g. in the first code below each instance of room will have struct vars of every roomtype)

Ques C : As in the actual code, type of rooms are too many. Is there a better way to make such code without checking for roomtype, everytime a function in room class want to edit properties of that particular roomtype, (e.g. by using interfaces or Polymorphic functions etc ?)
If yes, can you please provide a small sample code.

class home
{
	OnButtonClick()
	{
		switch(something)
		{
			case A : roomType = enumRoomType.bedroom;
			case B : roomType = enumRoomType.bathroom;
			....
		}
		Room room = new Room(roomType);
		listofRoom.Add(room);
	}
	
	writeAllRoomData()
	{
		foreach(room in listofRoom)
		{
			//How to access the appropriate data for each room ?
			//e.g. if listofRoom[0] is of type bedroom, access "bed"
		}
	}
	
	
}


class Room
{
	private enumRoomType roomType;
	
	public void Room (enumRoomType roomType)
	{
		this.roomType = roomType;
	}
	
	struct BedroomVars{
		public bool bed;
		public int lamps;
	}
	BedroomVars _bedroomVars;

	struct BathroomVars{
		public bool tub;
		public int soaps;
	}	
	BathroomVars _bathroomVars;
	
	struct kitchen{
	...
	}
	
	...

	public void SomeFunc();
	{
		switch(RoomType)
		{
			case enumRoomType.bedroom : 
				// Do something (maybe sex ... LOL:)
				_bedroomVars.bed = 1; break; 
			case enumRoomType.bathroom : 
				_bathroomVars.soaps = 2; break;
			...
		} 
	}
}


Another option is to inherit Room class for making different room classes

class Room
{
	private enumRoomType roomType;
	
	private BedroomVars _bedroomVars;
	private BathroomVars _bathroomVars;
		
	public void Room (enumRoomType roomType)
	{
		this.roomType = roomType;
		switch (roomType)
		{
			case enumRoomType.Bedroom : 
				_bedroomVars = new BedroomVars; break;
			case enumRoomType.Bathroom : 
				_bathroomVars = new BathroomVars; break;
		}
	}

	public void SomeFunc();
	{
        	switch(RoomType)
        	{
            		case enumRoomType.bedroom :
				// Do something 
                		_bedroomVars.bed = 1; break;
            		case enumRoomType.bathroom :
				 // Do something
                		_bathroomVars.soaps = 2; break;
	            ...
        	}
	}
}

public class BedroomVars : Room
{
	bool bed;
	int lamps;
}

public class BathroomVars : Room
{
	bool tub;
	int soaps;
}	


I will be very thankful for any help.
Posted
Updated 12-Sep-10 22:59pm
v2

1 solution

1. Right OO approach approach is the inheritance.

2. writeAllRoomData()
- declare this method as virtual in your base class (consider marking the base class as abstract so you must inherit from it) Room and override implementation in inherited class. Thereafter you can call at first base implementation (base.writeAllRoomData()) and extend it with specialized properties of inherited class.
- your Home will hold reference to an array (list) of Room objects which will be initialized with a specialized instance (such as Room newRom = new Kitchen()).

public abstract class Room
{
  public int Length { get;set;}
  public int Width { get;set;}

  public virtual void writeAllRoomData()
  {
    Console.WriteLine("Size: {0}x{1}", Length, Width);
  }
}

public class Kitchen: Room
{
  public bool HasCooker { get;set; }
  public override void writeAllRoomData()
  {
    base.writeAllRoomData();
    Console.writeAllRoomData("Has cooker: {0}", HasCooker);
  }
}

public class Home
{
  private List<Room> _rooms = new List<Room>();

  public List<Room> Rooms { get { return _rooms; }}

  public void writeHome()
  {
    Console.WriteLine("Home {");

    foreach(Room room in Rooms)
      room.writeAllRoomData();

    Console.WriteLine("}");
  }
}
 
Share this answer
 
v3
Comments
Abhishrek 13-Sep-10 21:50pm    
Thanks a lot voloda.
your answer seems good. I'll try this method in the original project :)
Thanks again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900