c# - 无法在对象中存储字典值

标签 c# object dictionary

所以我正在研究一个文本冒险的原型(prototype)。我在每个 Room 对象中创建了一个包含 availableExits 的 Dictionary,然后为原型(prototype)创建了一个 Room 对象数组。 房间(001 房间)在表单中正确加载,但我无法访问可用导出列表。经过一些调试后,我发现导出没有分配给 Room 对象。有人知道我在这里做错了什么吗?

代码摘要:

public RoomManager()
{
    //available exits for each room
    Dictionary<string, int> room1Exits = new Dictionary<string, int>();
    room1Exits.Add("E", 002);
    room1Exits.Add("S", 003);
    Dictionary<string, int> room2Exits = new Dictionary<string, int>();
    room2Exits.Add("S", 004);
    room2Exits.Add("W", 001);
    Dictionary<string, int> room3Exits = new Dictionary<string, int>();
    room3Exits.Add("N", 001);
    room3Exits.Add("E", 004);
    Dictionary<string, int> room4Exits = new Dictionary<string, int>();
    room4Exits.Add("N", 002);
    room4Exits.Add("W", 003);

    listOfRooms = new Room[5];
    listOfRooms[0] = new Room(0, "How the hell did you get here!?!", room1Exits);
    listOfRooms[1] = new Room(001, room1Desc, room1Exits);
    listOfRooms[2] = new Room(002, room2Desc, room2Exits);
    listOfRooms[3] = new Room(003, room3Desc, room3Exits);
    listOfRooms[4] = new Room(004, room4Desc, room4Exits);
}  

...

public class Room
{
    //Init Variables
    int roomNumber;
    string roomDescription;
    //Dictionary - index N,E,S,W will use room# for available exits and 000 for no exit
    Dictionary<string, int> availableExits = new Dictionary<string, int>();

    //Constructor
    //Need a Roomnumber, Room Description, and avilable exits
    public Room(int roomIndex, string basicRoomDescript,
                Dictionary<string, int> availableExits)
    {
        roomNumber = roomIndex;
        roomDescription = basicRoomDescript;
    }

    //Properties

    //Returns which exits can be chosen
    public Dictionary<string, int> AvailableExits
    {
        get { return availableExits; }
        set { AvailableExits = availableExits; }
    }
}   

...

public partial class Form1 : Form
{
    RoomManager level;
    Player player;
    //string CurrentRoom;
    Room CurrentRoom;
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
        level = new RoomManager();
        player = new Player("Victor", 001);
        CurrentRoom = level.RoomList[player.PlayerLocation];
        lblRoom.Text = "Room#: " + player.PlayerLocation;
        txtDesc.Text = CurrentRoom.GetRoomDescription();

        //Check for available exits and enable/disable buttons as needed
        this.SetExits();
    }

    //Private Methods

    private void SetExits() //might need to feed player and current room objects
    {
        if (!CurrentRoom.AvailableExits.ContainsKey("N"))
        { btnNorth.Enabled = false; }
        if (!CurrentRoom.AvailableExits.ContainsKey("E"))
        { btnEast.Enabled = false; }
        if (!CurrentRoom.AvailableExits.ContainsKey("S"))
        { btnSouth.Enabled = false; }
        if (!CurrentRoom.AvailableExits.ContainsKey("W"))
        { btnWest.Enabled = false; }
    }
}

我已经发布了项目 here .代码真的很粗糙,我今天早上刚把它砍下来,还没有做任何审查或清理。感谢您提供任何帮助/建议。

最佳答案

您需要在 Room 构造函数中设置 availableExits

public Room(int roomIndex, string basicRoomDescript, Dictionary<string, int> availableExits)
{
    roomNumber = roomIndex;
    roomDescription = basicRoomDescript;
    this.availableExits = availableExits;

}

关于c# - 无法在对象中存储字典值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957055/

相关文章:

javascript - 如何获取 javascript 中 for ( ... in ... ) 循环的位置?

python - 在defaultdict中把一个元素放在正确的位置

c++ - map 和 unordered_map 包含指向 double 的指针?

c# - 如何动态列出和访问 TabPage 对象

c++ - 尝试在 C++ 函数中添加数组的整数失败

java - 在代码中用 Java 创建和初始化 `HashMap<Date, Date>` 的最佳方法?

c# - 从 asp :DataList 中删除表和跨度标签

c# - 无法将匿名方法转换为类型 'System.Windows.Threading.DispatcherPriority',因为它不是委托(delegate)类型

c# - 我可以在 .NET 4 中序列化 ExpandoObject 吗?

c# - 将 Python 翻译成 C# - 随机数组