-4

我有一个包含以下代码的表格:

public partial class frmSalesTax : Form
{
    public frmSalesTax()
    {
        InitializeComponent();
    }

    //declare variables
    decimal ItemPrice = 00.00m;
    decimal TaxAmount = 00.08m;
    decimal TotalAmount = 00.00m;

    private void btnCalc_Click(object sender, EventArgs e)
    {
        try
        {
            if (decimal.TryParse(txtItemPrice.Text, out ItemPrice))
            {
                //Instantiated instance of a class here. 
                CTransaction Calc;
                Calc = new CTransaction();

                //set properties to calc tax amount.
                Calc.SalesTaxRate = .08m;
                Calc.TxtItemPrice = ItemPrice;

                //call the method in the instance of the class
                TaxAmount = Calc.CalculateTax();

                //Set tax amount property to be available for the calc.
                Calc.CalculateTax = TaxAmount;

                //call the method in the instance of the class.
                TotalAmount = Calc.CalculateTotal();

                //Display the values
                lblTaxAmt.Text = TaxAmount.ToString("c");
                lblTotal.Text = TotalAmount.ToString("c");
            }
            else
            {
                MessageBox.Show("Enter a numeric value please");
                txtItemPrice.Focus();
                txtItemPrice.SelectAll();

                lblTaxAmt.Text = string.Empty;
                lblEndTotal.Text = string.Empty;

            }
        }
        catch
        {
            MessageBox.Show("Critical Error");
        }
    }
    private void btnExit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}

和一个类:

public class CTransaction
{
    //Create private fields
    private decimal salesTaxRate = .07m;
    private decimal ItemPrice;
    private decimal taxAmount;

    //Define the properties
    public decimal SalesTaxRate
    {
        get { return salesTaxRate;}
        set { salesTaxRate = value;}
}
    public decimal TxtItemPrice
    {
        get { return ItemPrice; }
        set { ItemPrice = value;}
    }

    //Custom methods
    public decimal CalculateTax()
    {
        return ItemPrice * SalesTaxRate;
    }

    public decimal CalculateTotal()
    {
        return ItemPrice + taxAmount;
    }
}

我得到“无法分配给'CalculateTax',因为它是一个方法组。(Form1.cs .. 第 54 行 .. 第 21 列)

表单上有以下字段供用户与 txtItemPrice(文本框)交互 3 - 按钮(计算、清除、退出) lblTaxAmount(应显示我的税款如何应用于项目。lblEndTOtal(应为 itemPrice + 税额

4

2 回答 2

4

这是问题行:

                //Set tax amount property to be available for the calc.
                Calc.CalculateTax = TaxAmount;

您正在尝试将值 (TaxAmount) 分配给方法 (CalculateTax)。你不能那样做。如果您尝试设置税率,那么您需要添加一个公共属性以允许设置它:

Calc.TaxAmount = TaxAmount;

然后在您的 Calc 课程中:

public decimal TaxAmount
{
    get { return taxAmount; }
    set { taxAmount = value; }
}

然后一切都应该按您的预期工作。

于 2016-05-12T21:24:35.157 回答
0

您的 Calc.CalculateTax 行是一种方法,以便您通过方法传递值,您应该将其作为参数传递。

在您的代码中,我将对 CTransaction 类进行更改:

public decimal CalculateTotal(decimal taxAmount)
{
     return itemPrice + taxAmount;
}

而在你的frmSalesTax,你只需要删除你的行:

//Set tax amount property to be available for the calc.
                Calc.CalculateTax = TaxAmount;

然后在您的行中TotalAmount = Calc.CalculateTotal();,将taxAmount变量作为TotalAmount方法的参数。它应该是这样的:

TotalAmount = Calc.CalculateTotal(taxAmount);

它应该像你期望的那样工作。

有关更多信息,请查看以下链接:

C# 方法

C# 传递参数

于 2016-05-12T21:57:43.400 回答