I am currently working with classes and constructors. I have sint variable named current that equals 0 inside the constructor. Now when I click the button I am trying to increment property current and then call GetNextTree to display. But when incrementing current++ from button click I receive this error: does not exist in current context. What would be the proper way to increment current then?
public class fruit_trees
{
}
public class ListForTrees
{
public int current;
public fruit_trees GetNextTree()
{
current = 0;
fruit_trees ft = first_tree;
int i = 0;
while (i != current)
{
ft = ft.next_tree;
i++;
}
return ft;
}
}
private void ShowNextItem_Click(object sender, EventArgs e)
{
//Show Last Item
fruit_trees obj = mainlist.GetNextTree();
if (obj == null)
{
labelSpecificTree.Text = "No more trees!";
}
else
{
//error: current does not exist?
current++
labelSpecificTree.Text = obj.next_tree.GetTreeType.ToString();
}
}
The problem is in the scope (encapsulation) where you try to call
int current.From your posted code,
int currentis defined inclass ListForTrees. However you are trying to access it without initializing your object of type ListForTrees.In addition, you are using
mainlistwhich is also not defined in your posted code. Please, post complete code coverage of items that you use in code.