I am currently working with displaying a list of items. I have created a method GetNextItem that returns obj1. When I am calling the method from the buttonClick I get this following error:
Cannot implicitly convert type 'TreeFarm.Form1.fruit_trees' to 'string'.
Not sure why is doing such thing.
public items_list GetNextItem()
{
items_list obj1 = this.current_item;
if (obj1 != null)
{
current_item = current_item.next_item;
}
return obj1;
}
ListForItems mainlist = new ListForItems();
private void ShowNextItem_Click(object sender, EventArgs e)
{
labelSpecificItem.Text = mainlist.GetNextItem();
}
You’re trying to convert a value of type
items_listin to a string (.Textis ofStringtype). So, if this is one of your objects you can create an implicit cast operator or maybe try using.ToString()or explicitly casting it to a string using(String)mainlist.GetNextItem().if you desire this kind of assignment and
items_listis one of your objects, I would suggest the following addition to that class:Otherwise you’re going to have to rely on
ToString()getting it right.