简体   繁体   English

C#Dictionary - 字典中没有给定的键

[英]C# Dictionary - The given key was not present in the dictionary

I'm currently trying to load game objects from a Tiled (Tiled map editor) map file into a game engine I'm making in C#. 我目前正在尝试将Tiled(Tiled地图编辑器)地图文件中的游戏对象加载到我在C#中制作的游戏引擎中。 I'm using TiledSharp (Link to github here ). 我正在使用TiledSharp( 这里链接到github)。 It uses a dictionary to hold properties about each individual tile (or 'game object') I'm trying to load. 它使用字典来保存我正在尝试加载的每个单独的图块(或“游戏对象”)的属性。 But for some reason I get an error when I loop through the properties, and I also get an error if I check if it's null 但由于某些原因,当我遍历属性时出现错误,如果我检查它是否为空,我也会收到错误

Here's a snippet of the code I'm using: 这是我正在使用的代码片段:

for (int l = 0; l < tmxMap.Tilesets[k].Tiles.Count; l++)
    // This line throws an error
    if (tmxMap.Tilesets[k].Tiles[l].Properties != null)
        // and if I remove the above line, this line throws an error
        for (int m = 0; m < tmxMap.Tilesets[k].Tiles[l].Properties.Count; m++)

The error I get says The given key was not present in the dictionary. 我得到的错误说字典中没有给定的密钥。 But... I'm not even checking for a key. 但是......我甚至没有检查钥匙。

Am I missing something? 我错过了什么吗?

Any help would be appreciated. 任何帮助,将不胜感激。

The error I get says The given key was not present in the dictionary. 我得到的错误说字典中没有给定的密钥。 But... I'm not even checking for a key. 但是......我甚至没有检查钥匙。

Yes you are checking for a key. 是的,你正在检查钥匙。 This is your code: 这是你的代码:

if (tmxMap.Tilesets[k].Tiles[l].Properties != null)

You are checking for Tilesets with key k and then checking Tiles with key l . 您正在使用密钥k检查Tilesets ,然后使用密钥l检查Tiles If the Tilesets does not contain an item with key k , you will get that error. 如果Tilesets不包含带有键k的项,则会出现该错误。 The same is true for Tiles with key l . 具有键l Tiles也是如此。

You can do the following when working with dictionaries: 使用词典时,您可以执行以下操作:

Option 1 选项1

The lookup is performed twice: once to see if the item exists, and then a second time to get the value: 查找执行两次:一次查看项目是否存在,然后第二次获取值:

var items = new Dictionary<string, string>();
items.Add("OneKey", "OneValue");
if(items.ContainsKey("OneKey"))
{
    var val = items["OneKey"];
}

Option 2 选项2

And here is another approach where the lookup is performed once: 这是另一种执行查找的方法:

string tryVal;
if (items.TryGetValue("OneKey", out tryVal))
{
    // item with key exists so you can use the tryVal
}

As far as I can see in your code I think that Tiles is a Dictionary and when you try to iterate by tmxMap.Tilesets[k].Tiles[l] it throws error because it searches for key l, not for the l-element. 在我的代码中我可以看到,我认为Tiles是一个字典,当你尝试通过tmxMap.Tilesets[k].Tiles[l]进行迭代时,它会抛出错误,因为它搜索键l,而不是l元素。

You can try tmxMap.Tilesets[k].Tiles[tmxMap.Tilesets[k].Tiles.Keys.ElementAt(l)] 你可以试试tmxMap.Tilesets[k].Tiles[tmxMap.Tilesets[k].Tiles.Keys.ElementAt(l)]

You are trying to get a value based on keys k , l . 您正在尝试根据键kl获取值。

if (tmxMap.Tilesets[k].Tiles[l].Properties != null) statement is basically getting the value corresponding to the k key in Tilesets dictionary. if (tmxMap.Tilesets[k].Tiles[l].Properties != null)语句基本上是获取与Tilesets字典中的k键对应的值。 If Tilesets dictionary doesn't contain a value for the key k , an exception will be thrown. 如果Tilesets字典不包含键k的值,则抛出异常。 Also if there is no value corresponding to the l key, in Tiles dictionary, exception will be thrown. 此外,如果没有对应于l键的值,则在Tiles字典中,将抛出异常。

You can use TryGetValue extension method, which will give you the value if the item was found. 您可以使用TryGetValue扩展方法,如果找到该项,它将为您提供值。

    TileSet ts = null;

    if(tmxMap.Tilesets.TryGetValue(k, out ts)
    {
       for (int l = 0; l < ts.Tiles.Count; l++)
       { 
          Tiles tl = null;

          if(ts.TryGetValue(l,out tl)
          {
            if (tl.Properties != null)
            {
              for (int m = 0; m < tl.Properties.Count; m++)
              {
               //Do something
              }
            }
          }
        }
     }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 给定的键在词典中不存在。 C# - The given key was not present in the dictionary. c# C#:KeyNotFoundException:字典中不存在给定的键 - C#: KeyNotFoundException: The given key was not present in the dictionary “给定的密钥没有出现在字典中”nHibernate C# - “The given key was not present in the dictionary” nHibernate C# C#中的字典未处理异常:“字典中不存在给定键” - Dictionary Unhandled Exception in C#: “The given key was not present in the dictionary” c#当键本身是类型时,“给定键在词典中不存在” - c# “The given key was not present in the dictionary” when key is a type itself Xamarin.Forms - KeyNotFoundException: 字典中不存在给定的键 C# - Xamarin.Forms - KeyNotFoundException: The given key was not present in the dictionary C# KeyNotFoundException:字典中不存在给定的键(C#Unity ANDROID APP) - KeyNotFoundException: The given key was not present in the dictionary (C# Unity ANDROID APP) 与specflow c# 并行运行测试的字典中不存在给定的键 - The given key was not present in the dictionary running tests in parallel with specflow c# 字典查找CRM C#插件中不存在给定的键 - the given key was not present in the dictionary lookup CRM C# Plugin 字典中不存在给定的键。 mysql 数据库 C# 中的错误 - The given key was not present in the dictionary. error in mysql database c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM