C P*_*ton 2 c# dictionary equals keynotfoundexception
我准备好把头撞在墙上
我有一个名为Map的类,它有一个名为tiles的字典.
class Map
{
public Dictionary<Location, Tile> tiles = new Dictionary<Location, Tile>();
public Size mapSize;
public Map(Size size)
{
this.mapSize = size;
}
//etc...
Run Code Online (Sandbox Code Playgroud)
我暂时填写这本字典来测试一些东西..
public void FillTemp(Dictionary<int, Item> itemInfo)
{
Random r = new Random();
for(int i =0; i < mapSize.Width; i++)
{
for(int j=0; j<mapSize.Height; j++)
{
Location temp = new Location(i, j, 0);
int rint = r.Next(0, (itemInfo.Count - 1));
Tile t = new Tile(new Item(rint, rint));
tiles[temp] = t;
}
}
}
Run Code Online (Sandbox Code Playgroud)
并在我的主程序代码中
Map m = new Map(10, 10);
m.FillTemp(iInfo);
Tile t = m.GetTile(new Location(2, 2, 0)); //The problem line
Run Code Online (Sandbox Code Playgroud)
现在,如果我在我的代码中添加断点,我可以清楚地看到,我的地图类的实例(M)通过上面的函数充满了对,但是当我尝试用GetTile函数访问值:
public Tile GetTile(Location location)
{
if(this.tiles.ContainsKey(location))
{
return this.tiles[location];
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
它总是返回null.再次,如果我在Map对象中查看并找到位置键,其中x = 2,y = 2,z = 0,我清楚地看到值是FillTemp生成的Tile.
它为什么这样做?到目前为止,我对像这样的词典没有任何问题.我不知道它为什么返回null.再次,在调试时,我可以清楚地看到Map实例包含它说它没有的位置键......非常令人沮丧.
有线索吗?需要更多信息?
帮助将不胜感激:)
归档时间: |
|
查看次数: |
425 次 |
最近记录: |