Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using two separate string types in a list

Tags:

c#

Ok so for my C# programming class I am making an adventure game. I think I understand how to do most of it expect I'm having trouble setting up the "world".

I have a class for the world (World.cs) where I started creating a list for each room. However I'm confused of to add a name and description for each room.

For example if the List (room) is type String I would do room.Add("Prison", "This is a prison). What is the best way to do this?

like image 727
Serijag Avatar asked May 14 '26 22:05

Serijag


1 Answers

That's when you create a class

public class Room {
   public string Name { get; set; }
   public string Description { get; set; }
}

And hold a list of rooms:

List<Room> rooms = new List<Room>();

In order to add anything to the list, simply do this:

rooms.Add(new Room { Name = "Prison", Description = "This is a prison" });

Objects are used to group data together, this will allow for much cleaner code. It is one of the keystones of Object-Oriented Programming.

like image 50
Jeroen Vannevel Avatar answered May 17 '26 12:05

Jeroen Vannevel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!