I have a form with the following code :
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();
}
}
and a 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;
}
}
Im getting the "cannot assign to 'CalculateTax' because it is a method group. (Form1.cs .. line 54 .. column 21)
The form has the following fields on it for the user to interact with txtItemPrice (textbox) 3 - buttons (calc, clear, exit) lblTaxAmount (which should display how my tax is being applied to the item. lblEndTOtal (which should be the itemPrice + TaxAmount
This is the problem line:
//Set tax amount property to be available for the calc.
Calc.CalculateTax = TaxAmount;
You are trying to assign a value (TaxAmount) to a method (CalculateTax). You can't do that. If you are trying to set the tax rate then you need to add a public property to allow it to be set:
Calc.TaxAmount = TaxAmount;
Then in your Calc class:
public decimal TaxAmount
{
get { return taxAmount; }
set { taxAmount = value; }
}
Then everything should work as you expect.