0
    decimal aPrice = 15.75m;

    decimal aSales = decimal.Parse(txtASales.Text);

    decimal aRev = (aPrice * aSales);

    private void btnCalculate_Click(object sender, EventArgs e)
    {
        lblARev.Text = aRev.ToString();
    }

This is the code i have for it, what I'm trying to do is multiply the price of A-type tickets and the number of A-type tickets sold. The number sold will be taken from the user in a text box "txtASales" which I got from the object window, and renamed in the properties window (no coding done for that). Then the revenue will be shown on a label "lblARev". I am getting an error "A field initializer cannot reference the non-static field, method, or property" for txtASales, aPrice, and aSales.

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
  • 3
    possible duplicate of [A field initializer cannot reference the nonstatic field, method, or property](http://stackoverflow.com/questions/14439231/a-field-initializer-cannot-reference-the-nonstatic-field-method-or-property) – saarrrr Sep 13 '15 at 01:43

1 Answers1

2

You cannot reference txtASales, aPrice or aRev outside of a method that you create or one of the .NET events. You could do this:

private void btnCalculate_Click(object sender, EventArgs e)
{
    decimal aPrice = 15.75m;

    decimal aSales = decimal.Parse(txtASales.Text);

    decimal aRev = (aPrice * aSales);

    lblARev.Text = aRev.ToString();
}

But you cannot reference non-static(txtSales, aPrice, aRev) at the class level if they are not static from a field initializer.

thewisegod
  • 1,524
  • 1
  • 10
  • 11