'How do i fix error System.ArgumentOutOfRangeException ? get this error after execute calculation with the result of first calculation [duplicate]
i'm trying to create a calculator
After i rund the code... if i do a calculation like 9 x 9 i get result of 81
but when i try to use the result of 81 and divide by any number i get error System.ArgumentOutOfRangeException
9 x 9 = 81 then straigh / by any number i get error System.ArgumentOutOfRangeException
but
if i do type 81 and / by 9 i get correct answer of 9
the error comes after i do the first calculation...when i try to execute other calculation using the result i get that error
i tried to execute other calculation using the result of the first calculation
the code i try to execute is down below
namespace FormCalculator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonCopy_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox.Text);
}
private void buttonReset_Click(object sender, EventArgs e)
{
previousOperation = Operation.None;
textBox.Clear();
}
private void buttonClear_Click(object sender, EventArgs e)
{
if(textBox.Text.Length > 0)
{
double d;
if (!double.TryParse(textBox.Text[textBox.Text.Length - 1].ToString(), out d))
{
previousOperation = Operation.None;
}
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1, 1);
}
}
private void buttonDiv_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Div;
textBox.Text += (sender as Button).Text;
}
private void buttonMult_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Mul;
textBox.Text += (sender as Button).Text;
}
private void buttonSub_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Sub;
textBox.Text += (sender as Button).Text;
}
private void buttonAdd_Click(object sender, EventArgs e)
{
if (previousOperation != Operation.None)
PerformCalculation(previousOperation);
previousOperation = Operation.Add;
textBox.Text += (sender as Button).Text;
}
private void PerformCalculation(Operation previousOperation)
{
List<double> listNumbers = new List<double>();
switch (previousOperation)
{
case Operation.Add:
listNumbers = textBox.Text.Split('+').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] + listNumbers[1]).ToString();
break;
case Operation.Sub:
listNumbers = textBox.Text.Split('-').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] - listNumbers[1]).ToString();
break;
case Operation.Mul:
listNumbers = textBox.Text.Split('*').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] * listNumbers[1]).ToString();
break;
case Operation.Div:
try
{
listNumbers = textBox.Text.Split('/').Select(double.Parse).ToList();
textBox.Text = (listNumbers[0] / listNumbers[1]).ToString();
break;
}
catch (DivideByZeroException)
{
textBox.Text = "kkkkkkkkk";
throw;
}
case Operation.None:
break;
default:
break;
}
}
private void button1_Click(object sender, EventArgs e)
{
textBox.Text += (sender as Button).Text;
}
enum Operation
{
Add,
Sub,
Mul,
Div,
None
}
static Operation previousOperation = Operation.None;
private void buttonResult_Click(object sender, EventArgs e)
{
if (previousOperation == Operation.None)
return;
else
PerformCalculation(previousOperation);
}
}
}
Solution 1:[1]
I cannot recreate your issue with your code, but there are a few things you can improve that might help you find your issue when you're done.
Firstly, you do not need to catch DivideByZeroException with floating-point arithmetic. It will return infinity and will never throw an exception.
Secondly, if you wish to inform the user of an invalid math operation as a result of using a divide-by-zero operation you can change that code:
case Operation.Div:
listNumbers = textBox.Text.Split('/').Select(double.Parse).ToList();
string answer = listNumbers[1] != 0 ?
(listNumbers[0] / listNumbers[1]).ToString() : "Divide by zero";
textBox.Text = answer;
break;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ExplodatedFaces |
