I’m implementing backword function on a button. that when clicked moves to the previous link in the stack. the problem is this if it has one element in the stack pop() it gives an error of stack empty.
private void Backward_Click(object sender, EventArgs e)
{
try
{
if (simpleStack.Count != 0)
{
simpleStack.Pop();
string open = simpleStack.Pop();
PopulateListView(open);
complicatedStack.Push(open);
}
else if (simpleStack.Count == 0)
{
Backward.Enabled = false;
}
It works when I have more than one clicks n goes back to the previous item selected.but does not show the last one. I’m passing string in simpleStack. can anybody tell me what I’m missing?
Look at your code:
You’re popping twice, and ignoring the first result. Why would you do that? I suspect you can just remove the first
Popcall.Also note that your
elseclause doesn’t need to checksimpleStack.Count == 0– it must be, otherwise you wouldn’t be evaluating theelseclause. (Unless you’ve got multiple threads doing stuff of course – which wouldn’t be a good idea.)