"cannot assign because part of method group"
"cannot assign because part of 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();
}
}
和一个 class :
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
这是问题行:
//Set tax amount property to be available for the calc.
Calc.CalculateTax = TaxAmount;
您正在尝试将值 (TaxAmount) 分配给方法 (CalculateTax)。你不能那样做。如果您尝试设置税率,则需要添加一个 public 属性 以允许对其进行设置:
Calc.TaxAmount = TaxAmount;
然后在你的计算器中 class:
public decimal TaxAmount
{
get { return taxAmount; }
set { taxAmount = value; }
}
那么一切都应该如您所愿。
你的行Calc.CalculateTax是一个方法,为了让你通过一个方法传递一个值,你应该把它作为参数传递。
在您的代码中,我将对 CTransaction 进行更改 Class:
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);
它应该会像您期望的那样工作。
有关更多信息,请查看这些链接:
我有一个包含以下代码的表单:
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();
}
}
和一个 class :
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
这是问题行:
//Set tax amount property to be available for the calc.
Calc.CalculateTax = TaxAmount;
您正在尝试将值 (TaxAmount) 分配给方法 (CalculateTax)。你不能那样做。如果您尝试设置税率,则需要添加一个 public 属性 以允许对其进行设置:
Calc.TaxAmount = TaxAmount;
然后在你的计算器中 class:
public decimal TaxAmount
{
get { return taxAmount; }
set { taxAmount = value; }
}
那么一切都应该如您所愿。
你的行Calc.CalculateTax是一个方法,为了让你通过一个方法传递一个值,你应该把它作为参数传递。
在您的代码中,我将对 CTransaction 进行更改 Class:
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);
它应该会像您期望的那样工作。
有关更多信息,请查看这些链接: