I am working with lists. I am now experimenting with determining the positions of items inside list: first, next and last. I have been able to determine the first and last position of items in my list through function getPostion and displaying the item name through a Label. Three buttons in my form: ShowFirstItem ShowNextItem and ShowLastItem show the corresponding item in a label. I am having problems for displaying the next item. I have special method for next called GetNextTree. When I am calling this method from inside ShowNextItem click I get this error: Cannot implicitly convert type 'TreeFarm.Form1.fruit_trees' to 'string'. How can avoid this error and display the item currently pointed to?
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();
}
}
You need to convert your
fruit_treeto a string, and you may also want to consider thenullcase:EDIT: As MattSmith pointed out, perhaps you’d prefer to use
nextTree.GetTreeTypeinstead to match your other methods. It’s up to you.