【问题标题】:"cannot assign because part of method group" [closed]“由于属于方法组的一部分而无法分配” [关闭]
【发布时间】:2016-05-12 21:12:58
【问题描述】:

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

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

【问题讨论】:

  • CalculateTax 是一种方法。您不能为方法分配值。我不确定你的真正意图是什么。
  • 是的,@Jonesopolis 是对的,只需删除 Calc.CalculateTax = TaxAmount;
  • 我同意 - 在 btnCalc_Click 事件中

标签: c# class methods method-group


【解决方案1】:

这是问题所在:

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

那么一切都应该如你所愿。

【讨论】:

    【解决方案2】:

    您的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-27
      • 1970-01-01
      相关资源
      最近更新 更多