I am working with lists. I have been able to determine the first and last position of items in my list. I am using getPostion
and displaying item name through a
I'm not sure if this solves Your issue, but You can add
public fruit_trees current_tree;
field in ListForTrees
class.
Then, You can easily add method showing next and moving item pointer to the next item in the list. Remembering the current item allows You to avoid browsing the whole list each time You want next tree from the list. The code can look like this:
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;
}
In order to make it work, You should also remember to initiate the value of current_tree
with the first tree added to the list. You can also write some kind of ResetNextTreePointer()
method pointing it back to the first tree in the list.
Some more suggestions to the code:
I suppose it would be better to use properties for fields in your classes, so i.e. change
public fruit_trees first_tree;
into
private fruit_trees first_tree;
public fruit_trees First_tree { get { return first_tree; }
as it would disable any class outside your class to modify those fields explicitly.
This code:
if (count == 0)
{
...
}
else if (count != 0)
{
...
}
can be changed into:
if (count == 0)
{
...
}
else
{
...
}
I would add
public int current;
to the listoftrees. in the constructor set it to 0;
Then create a method to return the current fruit_trees object
public fruit_trees getCurrent()
{
fruit_trees ft = first_tree;
int i = 0;
while(i != current)
{
ft = ft.next_tree;
i++;
}
return ft;
}
now this method returns the current and not the next object. So you have 2 options in the next button event.
the quesiton here is do you want to move to the next tree object every time the button is clicked?
if so increment the current property and then call getcurrent to display. If you want the current to be retained and not move (i.e. clicking next over and over will result in the same thing)
then display getcurrent().next_tree.tree_type with no increment to current.