Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WP7-值不在预期范围内_C#_Windows Phone 7_Dictionary - Fatal编程技术网

C# WP7-值不在预期范围内

C# WP7-值不在预期范围内,c#,windows-phone-7,dictionary,C#,Windows Phone 7,Dictionary,我正在尝试将项目添加到字典中。我正在使用以下代码将值添加到字典中 Console.WriteLine("selectedCategories.Count==> " + selectedCategories.Count); selectedCategoryMap = new Dictionary<String, Category>(); if (selectedCategories != null) { foreach (Categor

我正在尝试将项目添加到字典中。我正在使用以下代码将值添加到字典中

Console.WriteLine("selectedCategories.Count==> " + selectedCategories.Count);
selectedCategoryMap = new Dictionary<String, Category>();
     if (selectedCategories != null)
       {
          foreach (Category category in selectedCategories)
            {
               selectedCategoryMap.Add(category.getCategoryID(), category);
            }
       }
我的分类课

 public class Category
    {

        private String categoryID;
        private String categoryName;

        public String getCategoryID()
        {
            return categoryID;
        }

        public void setCategoryID(String categoryID)
        {
            this.categoryID = categoryID;
        }

        public String getCategoryName()
        {
            return categoryName;
        }

        public void setCategoryName(String categoryName)
        {
            this.categoryName = categoryName;
        }
}

请告诉我哪里出错。

您如何定义您的
类别
类型?可能是问题的重复?现在我发现了问题。问题是两个项目的键值相同。这就是为什么我会犯这个错误。所有数据都是动态的。是否有任何方法可以使用字典中的相同键添加值?否。
字典中的键必须是唯一的。但是,您可以使用列表或其他集合作为值来存储每个键的多个项。噢。。谢谢@MártonMolnár。。我会朝这边看的。。谢谢分享知识…-)。。
 public class Category
    {

        private String categoryID;
        private String categoryName;

        public String getCategoryID()
        {
            return categoryID;
        }

        public void setCategoryID(String categoryID)
        {
            this.categoryID = categoryID;
        }

        public String getCategoryName()
        {
            return categoryName;
        }

        public void setCategoryName(String categoryName)
        {
            this.categoryName = categoryName;
        }
}