错误:无法隐式转换类型'无效'到'System.Collections.Generic.List“ [英] Error: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'
问题描述
我想从使用控制在.aspx设置我的.ascx控件的属性。
I am trying to set a property of my .ascx controls from an .aspx using that control.
因此,在我的一个的.aspx 有它这个控制,我有以下代码试图设置我的嵌入式的ItemsList财产的.ascx:
So in one of my .aspx which has this control in it, I have the following code trying to set the ItemsList property of my embedded .ascx:
Item item = GetItem(itemID);
myUsercontrol.ItemList = new List<Item>().Add(item);
这是我试图设置看起来像这样在的.ascx属性:
The property in the .ascx that I'm trying to set looks like this:
public List<Item> ItemsList
{
get { return this.itemsList; }
set { this.itemsList = value; }
}
错误:不能隐式转换类型'无效'到'System.Collections.Generic.List
Error: Cannot implicitly convert type 'void' to 'System.Collections.Generic.List'
所以我不明白的地方是越来越无效的财产的一部分?......奇怪的。
So I don't get where it's getting void as part of the property?...weird.
推荐答案
您不能这样做,因为add函数返回void,而不是到列表中的参考。你可以这样做:
You can't do that because the Add function returns void, not a reference to the list. You can do this:
mycontrol.ItemList = new List<Item>();
mycontrol.ItemList.Add(item);
或使用的collection初始化:
mycontrol.ItemList = new List<Item> { item };
这篇关于错误:无法隐式转换类型'无效'到'System.Collections.Generic.List“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!