将变量传递给另一个类,执行计算然后将其传回

时间:2017-09-04 22:00:04

标签: c#

我正在尝试编写一个简单的计算器,但是阅读和收听不同的教程我应该遵循OOP方法。试图而不是在按钮点击方法中拥有我的所有代码我认为逻辑方法是将所有计算都放在一个表单中,例如添加除法,百分比和form1类将变量值传递给所有逻辑发生的计算类。但由于某种原因,总数总是等于我输入的数字,而不是总数+总数的总和。任何帮助表示赞赏。

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 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;
    }        
}

3 个答案:

答案 0 :(得分:2)

在GetPrice()中

,它应该是total += total; 不是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

答案 1 :(得分:0)

我为这种方法制作了一个控制台应用程序。很容易实现它:

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。

通过这种方式,保存每次通话的实例,您将能够通过使用属性将您输入的最后一个值相加

答案 2 :(得分: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;
    }        
}