【发布时间】:2012-04-12 12:49:04
【问题描述】:
当我使用 f10 调试代码时,它可以正常工作,没有错误!但是在运行时我收到了这个错误: "已添加具有相同密钥的项目"
请帮忙
我的字典:
public static Dictionary<string, string> ImageFilePath
= new Dictionary<string, string>();
在同一个 Glob.cs 中使用我的函数:
public static Image ShowImageOnColumn(string Value,byte ImageHeigth,byte ImageWidth)
{
.
.
.
string FilePath = "",ImgId = "";
Image img_ = new Image();
Random rnd = new Random();
ImgId = rnd.Next(100000000).ToString();
img_.ImageUrl = "ShowImageInRuntime.aspx?FileName=" + ImgId;
ImageFilePath.Add(ImgId, FilePath);
img_.Height = Unit.Pixel(ImageHeigth);
img_.Width = Unit.Pixel(ImageWidth);
return img_;
}
【问题讨论】:
-
字典不能包含重复的键,您的 Random 会生成重复的
ImgIds。 -
在“添加”之前放置消息框,显示所有带有排序的项目并运行您的应用程序。我认为确实有重复的项目
-
尝试用
Guid.NewGuid().ToString("N")替换随机生成代码
标签: c# asp.net dictionary