I am working with a link list. My constructor takes arrays with items in it or appends an item(one at time) to the list through BtnAddTree click. For experimental reasons I am trying to figure out a way to retrieve the position of an item from the list. I have set three buttons that will hopefully show: first, next, last item and display through a label. I created two functions to achieve my need Retrieve and Current_Tree() but here is where I come to a dead end. I am aware to display items that are next I may need to use a loop but unsure how to go about it. How can I display the position of an item found in the list?
public class ListForTrees
{
//Retrieve Position of item
public void Retrieve(int Position)
{
int new_position = Position;
fruit_trees current = first_tree;
for (int i = 0; i < Position && current != null; i++)
{
current = current.next_tree;
}
return current;
}
//Show Current Tree
public void Current_Tree()
{
try
{
current = fruit_trees.first_tree;
labelSpecificTree.Text = current.Type.ToString();
}
catch { };
}
}
ListForTrees mainlist = new ListForTrees();
private void BtnGo_Click(object sender, EventArgs e)
{
fruit_trees[] ax = { new fruit_trees("cherry", 48, 12.95, 3),
new fruit_trees("pine", 36, 9.95, 8),
new fruit_trees("oak", 60, 14.95, 2),
new fruit_trees("peach", 54, 19.95, 3),
new fruit_trees("pear", 36, 11.85, 2),
new fruit_trees("apple", 62, 13.45, 5)
};
mainlist = new ListForTrees(ax);
fruit_trees current = mainlist.first_tree;
while (current != null)
{
TxtOutput.AppendText(current.ToString() + Environment.NewLine);
current = current.next_tree;
}
}
private void ShowNextItem_Click(object sender, EventArgs e)
{
//Show Next Item
}
private void ShowLastItem_Click(object sender, EventArgs e)
{
//Show Last Item
}
private void ShowFirstItem_Click_1(object sender, EventArgs e)
{
// Show First Item
}
}
}
Develop a way to compare the object properly. is tree type unique? maybe you need a full icomparable?
then create a method in the listoftrees class
also as a side note ..Dont set the label value in the class in the method Current_Tree(). That should return a fruit_trees object and the btn click methods should set the label value .
you should actually just get rid of that method completely(unless there is some reason for it outside of the code above.