I am trying to set a property of my .ascx controls from an .aspx using that control.
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);
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; }
}
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.
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);
or use a collection initializer:
mycontrol.ItemList = new List<Item> { item };
After creating the List<Item>
you're immediately calling Add on it, which is a method returning void. This cannot be converted to the type of ItemList.ItemList.
You should do this instead:
var list = new List<Item>();
list.Add(item);
ItemList.ItemList = list;
new List<Item>().Add(item);
This line returns void.
Try:
var list = new List<Item>();
list.Add(item);
ItemListt.ItemList = list;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With