lundi 2 mars 2015

C# Inheritance stackoverflow exception

Acces to list in Form1 without creating a new instance


Fix : passed my listboxes and lists in the constructor of the stack class. Removed inheritance of Form1.


I'm currently working on a calculator with reverse polish notation to learn about stacks, abstract classes, inheritance etc. I get a stackoverflow error when i try to inherit from Form1 in my abstract class.


The question is: Is there a way to prevent this error from happening and get acces to two lists in my Form1? When i use Form1 form1 = new Form1(); I create a new instance of Form1, and the result is an empty list, so i was thinking about inheriting Form1.


I inherit from this class, because I'm trying to reach this two lists in my Form 1:


In the Form1 class i've got 2 lists:



public List<string> lastEventList = new List<string>();
public List<string> stackDisplayList = new List<string>();


This is my abstract class:



using System;
using System.Windows.Forms;

namespace HP_calculator
{
abstract class Stack : Form1
{
ListBox lastEvent, stackDisplay;
public Stack() { }
public Stack(ListBox lastEvent, ListBox stackDisplay)
{
this.lastEvent = lastEvent;
this.stackDisplay = stackDisplay;
}
public abstract int Pop();
public virtual void Push(int i)
{
//how to acces list
lastEventList.Add("Pushed " + i + " on stack");
((CurrencyManager)lastEvent.BindingContext[lastEventList]).Refresh();
string addItem = String.Format("[{0}] \t -> \t {1}", 11 - Count(), i);
stackDisplayList.Add(addItem);
((CurrencyManager)stackDisplay.BindingContext[stackDisplayList]).Refresh();
}
public abstract int Count();
}
}


My Form1 class:



using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HP_calculator
{
public partial class Form1 : Form
{
Stack stack;
public List<string> lastEventList = new List<string>();
public List<string> stackDisplayList = new List<string>();
public Form1()
{
InitializeComponent();
stackChoice.SelectedIndex = 0;
lastEvent.DataSource = lastEventList;
stackDisplay.DataSource = stackDisplayList;
}

private void zero_Click(object sender, EventArgs e)
{
numberInput.AppendText("0");
}

private void one_Click(object sender, EventArgs e)
{
numberInput.AppendText("1");
}

private void two_Click(object sender, EventArgs e)
{
numberInput.AppendText("2");
}

private void three_Click(object sender, EventArgs e)
{
numberInput.AppendText("3");
}

private void four_Click(object sender, EventArgs e)
{
numberInput.AppendText("4");
}

private void five_Click(object sender, EventArgs e)
{
numberInput.AppendText("5");
}

private void six_Click(object sender, EventArgs e)
{
numberInput.AppendText("6");
}

private void seven_Click(object sender, EventArgs e)
{
numberInput.AppendText("7");
}

private void eight_Click(object sender, EventArgs e)
{
numberInput.AppendText("8");
}

private void nine_Click(object sender, EventArgs e)
{
numberInput.AppendText("9");
}

private void clear_Click(object sender, EventArgs e)
{
////////////Make a call to a clear method
//Clear stack numberinput etc.
stack = null;
NewStack();
numberInput.Clear();
lastEventList.Add("Stack cleared");
((CurrencyManager)lastEvent.BindingContext[lastEventList]).Refresh();
}
private void plus_Click(object sender, EventArgs e)
{
stack.Push(stack.Pop() + stack.Pop());
}

private void minus_Click(object sender, EventArgs e)
{
stack.Push(stack.Pop() - stack.Pop());
}

private void multiply_Click(object sender, EventArgs e)
{
stack.Push(stack.Pop() * stack.Pop());
}

private void divide_Click(object sender, EventArgs e)
{
//delen door 0 kan niet

int a = stack.Pop();
int b = stack.Pop();
if (a != 0)
{
stack.Push(a + b);
}
else { MessageBox.Show("Poging om door 0 te delen!", "Invoer error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}

private void enter_Click(object sender, EventArgs e)
{
//add to selected stack
try
{
//fix adding in displays after error
int number = Convert.ToInt32(numberInput.Text);
stack.Push(number);
//als het gelukt is, maak dan een call naar deze methode
//add stack content to stackDisplay
//add index
lastEvent.SelectedIndex = lastEvent.Items.Count - 1;
numberInput.Clear();
}
catch
{
MessageBox.Show("De invoer is ongeldig!", "Invoer error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void stackChoice_SelectedIndexChanged(object sender, EventArgs e)
{
//zorg ervoor dat je ondertussen kunt switchen en de stack dus wordt overgezet
List<int> temporary = new List<int>();
if (stack != null)
{
while (stack.Count() > 0)
{
temporary.Add(stack.Pop());
}
}
NewStack();
for(int i = temporary.Count -1; i >= 0; i--){
stack.Push(temporary.Last());
temporary.RemoveAt(i);
}
}
private void NewStack()
{
if (stackChoice.Text == "My list stack") { stack = new MyListStack(lastEvent,stackDisplay); }
if (stackChoice.Text == "List stack") { stack = new ListStack(lastEvent, stackDisplay); }
if (stackChoice.Text == "Array stack") { stack = new ArrayStack(lastEvent, stackDisplay); }
}
}
}


An example of another class:



using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace HP_calculator
{
class ListStack : Stack
{
private List<int> stack;
public ListStack(ListBox lastEvent, ListBox stackDisplay)
: base(lastEvent, stackDisplay)
{
this.stack = new List<int>();
this.stack.Capacity = 10;
}
public override int Pop()
{
int result = -1;
if(stack.Count > 0)
{
result = stack.LastOrDefault();
stack.RemoveAt(stack.Count -1);
}
else
{
MessageBox.Show("De stack is leeg!", "Error in de stack", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
return result;
}
public override void Push(int i)
{
if (stack.Count < stack.Capacity)
{
stack.Add(i);
base.Push(i);
}
else
{
MessageBox.Show("De stack zit vol!", "Error in de stack", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public override int Count()
{
return stack.Count;
}
}
}


Do you know how to prevent the stackoverflow error and get acces to those lists?


Aucun commentaire:

Enregistrer un commentaire