Looping-Messagebox !!
Ich habe eine Schleife Messagebox nach einem Fehler auftritt und ich frage mich, wie es zu beheben. Ich habe versucht, die Calculate() Methode zurückzugeben und ich denke, das ist das Problem, aber ich bin nicht sicher.C# Looping-Messagebox für Ausnahme
namespace WindowsFormsApplication7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int division = 0;
private void Form1_Load(object sender, EventArgs e)
{
}
private decimal Calculate()
{
// This array is to hold the logical operators
string[] allowed = { "+", "-", "*", "/" };
// If the right operator is selceted then perform the action and return result
if (operate.Text == "+")
{
decimal division = Convert.ToDecimal(operand1.Text) + Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "-")
{
decimal division = Convert.ToDecimal(operand1.Text) - Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "*")
{
decimal division = Convert.ToDecimal(operand1.Text) * Convert.ToDecimal(operand2.Text);
}
else if (operate.Text == "/")
{
decimal division = (Convert.ToDecimal(operand1.Text)/Convert.ToDecimal(operand2.Text));
}
// if the operator is not something within the array then display message
else if (!allowed.Contains(operate.Text))
{
string msg = string.Format("Not a valid operater {0}Please Enter one of the Following:{0}{1}"
, Environment.NewLine, string.Join(Environment.NewLine, allowed));
MessageBox.Show(msg);
operate.Text = "";
}
return Calculate();
}
Sie versuchen müssen 'division' an der Spitze Ihrer Methode zu erklären und zurück, dass der Boden an. – LarsTech
'return Calculate();' Rekursiv ruft die Funktion für immer auf, –