I try to code a simple calculator, but I read and listen to different textbooks. I have to follow the OOP method. Trying instead of having all my code in a button click method, I thought the logical approach was to have all the calculations in one form, for example. add divide, percent and class form1 pass the values โโof the variables to the computation class, where all the logic happens. But for some reason, the sum is always equal to the number I entered last, and not the sum of the total + total. Any help is appreciated.
namespace calculator
{
public partial class calculator : Form
{
public calculator()
{
InitializeComponent();
}
private void btnInput_Click(object sender, EventArgs e)
{
Calculations calculations = new Calculations();
calculations.total = Convert.ToInt32(txtPrice.Text);
calculations.GetPrice();
displayBox.Text = Convert.ToString(calculations.total);
}
}
}
Class of calculations
class Calculations
{
public int total;
public int GetPrice()
{
total =+ total;
String totalCheck = Convert.ToString(total);
System.Windows.Forms.MessageBox.Show(totalCheck);
return total;
}
}
source
share