0

I am currently making a game with randomly generated rooms, as I iterate through the newly created rooms to check their coordinates to add them onto a graph I have a list of rooms.

list<Room> listOfAllRooms;

I can continue with the clunky way of specifying where all the room objects are stored.

list<Room> listOfAllRooms = new List<Room> {roomObject.room1, ...}

They are stored in another class just to keep things tidy, exactly like so...

public class RoomObjects { public static Room room1 = new Room(); .....}

All the way for 25 rooms.

This is messy beyond all belief and will create a serious issue should I expand beyond the current 25 rooms I am currently working with. In my mind there should exist a way to populate this list of all the rooms directly from one neat and tidy source. So really my question boils down to is there a better way to declare these objects and store them into a list automatically? or if not at least a way to add each room into a list automatically?

(Note, I have a large part of the code working with a list of all rooms so i hope i don't have to re-write that to fix this)

6
  • I would suggest you to use ObservableCollection<Room> instead of List<Room> would be easier. Commented Nov 18, 2015 at 5:58
  • What if you provide a method in that class that can add the instances to a list and return that list? Commented Nov 18, 2015 at 6:03
  • why not use list instead of room1,room2,room3....? Commented Nov 18, 2015 at 6:03
  • Why do you use static rooms adding them to a non-static list? Why do you need every single room seperately? Commented Nov 18, 2015 at 6:03
  • you want to populate your class RoomObjects or list<Room>? Commented Nov 18, 2015 at 6:04

2 Answers 2

5

Your Room class:

public class Room
{
    public string NameRoom { get; set; }
}

To create objects of Room class, it is possible to use pattern Factory Pattern. With this pattern, we develop an abstraction that isolates the logic for creating of class to create. The pattern has really simple implementation, see:

public static Room FactoryOfRoom(string roomName)
{
    return new Room() { NameRoom=roomName };
}

And this code is where you can populate your list by Factory Pattern pattern:

static void Main(string[] args)
{
   List<Room> listRoom = new List<Room>();
   for (int i = 0; i < 25; i++)
   {
       listRoom.Add(FactoryOfRoom(i.ToString()));
   }       
}

If it is not what you want, feel free to correct me.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow, Thank you... I hadn't even thought of the idea to use logic for the creation of the rooms, This is a perfect solution.
@Grey read more about patterns:). It is real treasure of programming world. For example, the best book I've ever read is en.wikipedia.org/wiki/Design_Patterns and this link is very helpful csharpdesignpatterns.codeplex.com
I appreciate the codeplex link to design patterns in C#4. I didn't come across this yet :)
0

Though not elegant solution but in your case using reflection until the time we refactor or change design, we can prepare list of available rooms suppose in case room1, room2 etc., were used at many places directly instead of using from List<Room> but the new code using List<Room>.

I emphasize that this is just to show alternate possibility:

public class RoomObjects 
{
   public static Room room1 = new Room { Id = 1 }; 
   public static Room room2 = new Room();
   public static int room3 = 0; //Took int type for test that Room & Int32 type castings don't mess up while reflecting

   public List<Room> GetRoomObjects()
   {
       List<Room> availableRooms = new List<Room>();

       var allRoomFields = this.GetType().GetFields(); //GetProperties() in case of properties we want to reflect over.

       foreach (var roomFieldInfo in allRoomFields)
       {
           Room tempRoom = roomFieldInfo.GetValue(this) as Room; //Here Int32 field won't throw invalid cast exception while casting to Room type

           if (tempRoom != null)
           {
               availableRooms.Add(tempRoom);
           }
       }

       return availableRooms;
   }
}

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.