Passing the variable to another class, do the calculation, then pass it back

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;

        //See whats being stored in total
        String totalCheck = Convert.ToString(total);
        System.Windows.Forms.MessageBox.Show(totalCheck);                    

        return total;
    }        
}
+4
source share
3 answers

in GetPrice (), it should total += total; not be total =+ total;.

@Tipx.

- , https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/index += =+.

x + = y - . y x, x .

+ x - x

=+, x =+ y x = +y.

+2

. :

namespace Calculations
{
    public class Program
    {
        private static List<Calculations> calcs = new List<Calculations>();

        public static void Main(string[] args)
        {
            Console.WriteLine("In this program you will put 10 different prices and a value will be returned.");
            Console.WriteLine();
            for (int i = 0; i < 10; ++i)
            {
                Console.Write("Enter a price: ");
                Calculations calc = new Calculations(int.Parse(Console.ReadLine()));
                calc.GetPrice();
                calcs.Add(calc); //This is for example if you want to modify or re-access them
            }
        }
    }

    public class Calculations
    {
        private static Calculations instance;
        private static int _total;

        private static int total
        {
            get
            {
                return _total;
            }
            set
            {
                //The logic happens here.
                instance.actsubtotal = value;
                instance.acttotal = _total;
                _total += value; //The single value will be summed to a stored absolute total.
            }
        }

        //actsubtotal: Is the actual subtotal you entered.
        //acttotal: Is the total that was stored in each case, if you enter for example:
        //--- 30, 20, 50, you will have 3 instances, and the value of each acttotal will be: 30, 50, 100
        public int actsubtotal,
                   acttotal;

        public Calculations(int subtotal)
        {
            instance = this; //There is the magic, with this, you will tell the property where to find the last value.
            total = subtotal; //Pass it as a single value (without summing it) 
        }

        public void GetPrice()
        {
            Console.WriteLine("-------------");
            Console.WriteLine();
            Console.WriteLine("You actually entered: {0}", actsubtotal);
            Console.WriteLine("Your current total is: {0}", total);
            Console.WriteLine();
            Console.WriteLine("-------------");
        }
    }
}

, , , .

, , , , , MessageBox.

, , , .

0

, , , (, , , , ):

public partial class calculator : Form
{
    public calculator()
    {
        InitializeComponent();
    }
       //assuming this is clicking the Add (+) Button
    private void btnInput_Click(object sender, EventArgs e)
    {
        double fNum = 0.0; //initialize a variable with default value
                           //if you didn't include 0.0 you will get a "local variable" error

        Calculations calculations = new Calculations();
        calculations.Total = Convert.ToInt32(txtPrice.Text);

        fNum = calculations.GetPrice();

        //take Note the below code will really just return the value you have just entered in your textbox since you didn't used calculations.GetPrice();
        //displayBox.Text = Convert.ToString(calculations.Total);

        //the below code will show the value you have entered added
        //to itself since the logic from your class is just Total+=Total
        // example I input 1. (so Total = 1) then fNum will be (Total = 1 + 1)
        // which is 2
        displayBox.Text = Convert.ToString(fNum);


    }   
}



public class Calculations
{
    public int Total {get; set;} //Best practice to use CamelCase for properties inside a class

    public int GetPrice()
    {
        Total += Total;

        return Total;
    }        
}
0

Source: https://habr.com/ru/post/1685046/


All Articles