gpt4 book ai didi

c# - 项目的位置和 'Cannot implicitly convert type to string error'

转载 作者:行者123 更新时间:2023-11-30 22:22:21 24 4
gpt4 key购买 nike

我正在处理列表。我现在正在尝试确定列表中项目的位置:firstnextlast。我已经能够通过函数 getPostion 确定列表中项目的第一个和最后一个位置,并通过标签显示项目名称。我表单中的三个按钮:ShowFirstItem ShowNextItem 和 ShowLastItem 在标签中显示相应的项目。我在显示下一项时遇到问题。 next 我有一个特殊的方法叫做 GetNextTree。当我从 ShowNextItem 内部调用此方法时,单击我收到此错误:Cannot implicitly convert type 'TreeFarm.Form1.fruit_trees' to 'string'. 如何避免此错误并显示当前指向的项目?

namespace TreeFarm {
public partial class Form1: Form {
public Form1() {
InitializeComponent();
}

public class ListForTrees {


public fruit_trees GetNextTree() {
//save currently pointed tree
fruit_trees tree = this.current_tree;
if (tree != null) {
//move current_tree to the next tree, to be returned next time
current_tree = current_tree.next_tree;
//return saved tree object
}
return tree;
}

}
private void ShowNextItem_Click(object sender, EventArgs e) {
//Show Next Item
labelSpecificTree.Text = mainlist.GetNextTree();
}

}

最佳答案

这是因为您的 GetNextTree() 方法返回一个 fruit_trees 对象,但您试图将该返回值设置为 ShowNextItem 内部的一个字符串(labelSpecificTree 的 Text 属性)。你需要做这样的事情:

private void ShowNextItem_Click(object sender, EventArgs e)
{
//Show Next Item
labelSpecificTree.Text = mainlist.GetNextTree().ToString();
}

关于c# - 项目的位置和 'Cannot implicitly convert type to string error',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13871678/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com