我目前正在处理显示项目列表。我创建了一个GetNextItem返回obj1. 当我从 调用该方法时,出现buttonClick以下错误:
Cannot implicitly convert type 'TreeFarm.Form1.fruit_trees' to 'string'.
Run Code Online (Sandbox Code Playgroud)
不知道为什么要这样做。
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();
}
Run Code Online (Sandbox Code Playgroud)
您正在尝试将类型为items_listin的值转换为字符串(.Textis of Stringtype )。因此,如果这是您的对象之一,您可以创建一个隐式转换运算符,或者尝试使用.ToString()或使用(String)mainlist.GetNextItem().
如果您想要这种分配并且items_list是您的对象之一,我建议在该类中添加以下内容:
public static implicit operator String(items_list itemslist)
{
return /* however you want to portray itemslist as a string */;
}
Run Code Online (Sandbox Code Playgroud)
否则,您将不得不依靠ToString()正确处理。