“因为方法组的一部分而无法分配”

时间:2016-05-12 21:12:59

标签: c# class methods method-group

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

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 + TaxAmount

2 个答案:

答案 0 :(得分:4)

这是问题:

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

您正在尝试为方法(CalculateTax)分配值(TaxAmount)。你不能这样做。如果您尝试设置税率,则需要添加公共属性以允许设置:

Calc.TaxAmount = TaxAmount;

然后在你的Calc课程中:

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

然后一切都应该如你所愿。

答案 1 :(得分: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# Methods

C# Passing Parameters