无法在对象中存储字典值

时间:2012-01-21 21:55:11

标签: c# object dictionary

所以我正在寻找文本冒险的原型。我在每个Dictionary对象中创建了Room个availableExits,然后为原型创建了一个Room个对象数组。 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。代码非常粗糙,我今天早上刚刚修好了,还没有做任何评论或清理。任何和所有帮助/建议表示赞赏。

4 个答案:

答案 0 :(得分:5)

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

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

}

答案 1 :(得分:3)

您没有在构造函数中分配成员变量。

答案 2 :(得分:3)

你的availableExits类中有一个名为Room的局部变量,你将一个名为availableExits的参数传递给构造函数,但是你永远不会将局部变量赋给传递给它的参数。构造

因此,局部变量的值始终是空字典,因为您确实将局部变量指定为新字典。

此外,您对AvailableExits属性上的setter的定义似乎是递归的,会导致StackOverflowException

应该如下:

public Dictionary<string, int> AvailableExits
{
    get { return availableExits; }
    set { availableExits = value; }
}

最好使用自动实现的属性代替这样的简单属性。

答案 3 :(得分:1)

问题在于您将availableExits字典传递给房间构造函数,但是您没有对传入的数据执行任何操作。您需要将传入的字典的内容复制到类成员availableExits中,或者您需要将传递给构造函数的映射视为已更改所有权,而不是在类中分配映射,而是在构造函数中分配this.availaleExits。