C# .NET - ItemCollection 作为 IList传递时未编译
我无法让代码片段#2 工作,所以我必须将逻辑更改为代码片段#1。有人可以解释一下为什么代码片段 #2 不起作用吗?我认为会的,因为 ItemCollection 实现了 IList 接口...我知道每个列表框中存储的类型(listbox1 是字符串,listbox2 是 PictureInfo 类型)。
(注:PictureInfo是我自己定义的类型)
1:
private void MoveImagesFromHCollectionToCollection(ItemCollection collectionMoveFrom, ItemCollection collectionMoveTo)
{
//Loop through All strings in the lstAll list box. Then use each picture path to convert
//each picture into their own class
foreach (string file in collectionMoveFrom)
{
PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);
collectionMoveTo.Add(mp);
collectionMoveFrom.Remove(file);
//...
}
}
//Method call...
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);
2:
private void MoveImagesFromHCollectionToCollection(IList<string> collectionMoveFrom, IList<PictureInfo> collectionMoveTo)
{
//Loop through All strings in the lstAll list box. Then use each picture path to convert
//each picture into their own class
foreach (string file in collectionMoveFrom)
{
PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);
collectionMoveTo.Add(mp);
collectionMoveFrom.Remove(file);
//...
}
}
//Method call...parameter 1 Listbox contains strings, parameter 2 Listbox contains objects of type PictureInfo
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);
I couldn't get code snippet #2 to work, so I had to change my logic to code snippet #1. Can someone please explain why code snippet #2 wouldn't work? I thought it would, since ItemCollection implements the IList interface... I know the types that are stored in each listbox (listbox1 is string, and listbox2 is PictureInfo type).
(Note: PictureInfo is my own defined type)
1:
private void MoveImagesFromHCollectionToCollection(ItemCollection collectionMoveFrom, ItemCollection collectionMoveTo)
{
//Loop through All strings in the lstAll list box. Then use each picture path to convert
//each picture into their own class
foreach (string file in collectionMoveFrom)
{
PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);
collectionMoveTo.Add(mp);
collectionMoveFrom.Remove(file);
//...
}
}
//Method call...
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);
2:
private void MoveImagesFromHCollectionToCollection(IList<string> collectionMoveFrom, IList<PictureInfo> collectionMoveTo)
{
//Loop through All strings in the lstAll list box. Then use each picture path to convert
//each picture into their own class
foreach (string file in collectionMoveFrom)
{
PictureInfo mp = ReturnPictureInfoTypeBasedOffFileExtension(img, file);
collectionMoveTo.Add(mp);
collectionMoveFrom.Remove(file);
//...
}
}
//Method call...parameter 1 Listbox contains strings, parameter 2 Listbox contains objects of type PictureInfo
MoveImagesFromHCollectionToCollection(listbox1.Items, listbox2.Items);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
ItemCollection
未实现IList
。IList
(非通用)是一个不同的接口。ItemCollection
does not implementIList<string>
.IList
(non-generic) is a different interface.它继承自
IList
接口,但不继承IList
接口。它们是不同的,因为后者接受特定类型的对象。IList 文档
IList< T>文档
It inherits from the
IList
interface but not theIList<T>
interface. They are different because the latter accepts a specific type of object.IList Documentation
IList< T > Documentation