I've been having difficulty fixing my equals function
Let's say I enter the operation 3 + 4, the result is 7 then 11 and so on. Working as intended same goes for multiplication.
However if I enter the operation 12 / 2 the result is 6 but when pressed again I receive 2 and pressing again gives 6, so obviously instead of calculating again I'm receiving the values of the next operation. This happens with subtraction as well.
Here's the following code:
private void BtnCommonOperation_Clicked(object sender, System.EventArgs e)
{
var button = (Button)sender;
isOperatorClicked = true;
operatorName = button.Text;
firstNumber = Convert.ToDecimal(CalcResult.Text);
}
public decimal Calculate(decimal firstNumber, decimal secondNumber)
{
decimal result = 0;
if (operatorName == "+")
{
return firstNumber + secondNumber;
}
else if (operatorName == "-")
{
return firstNumber - secondNumber;
}
else if (operatorName == "*")
{
return firstNumber * secondNumber;
}
else if (operatorName == "/")
{
return firstNumber / secondNumber;
}
return result;
}
private void BtnEquals_Clicked(object sender, System.EventArgs e)
{
try
{
decimal secondNumber = Convert.ToDecimal(CalcResult.Text);
string finalResult = Calculate(firstNumber, secondNumber).ToString("");
CalcResult.Text = finalResult;
}
catch (Exception ex)
{
DisplayAlert("Error", ex.Message, "Ok");
}
}
What I have tried:
I've tried inverting the order of firstNumber and secondNumber in the / and - operations and it seems to fix this issue, but will return me incorrect calculations of course for being out of order. If anyone can help me I'd be so happy.