a.Amount += 10;

  • Thread starter Thread starter Gustaf Liljegren
  • Start date Start date
G

Gustaf Liljegren

I guess this is a newbie question. I got an Account class with an Amount
property, like this:

public float Amount
{
get { return amount; }
set { amount = value; }
}

I need to add to this amount sometimes, but when I write like this

a.Amount += 10;

I get the error "The left-hand side of an assignment must be a variable,
property or indexer". Since a.Amount is a property, I don't understand the
complaint. Is this supposed to work or not?

Thanks,

Gustaf
 
Gustaf Liljegren said:
I guess this is a newbie question. I got an Account class with an Amount
property, like this:

public float Amount
{
get { return amount; }
set { amount = value; }
}

I need to add to this amount sometimes, but when I write like this

a.Amount += 10;

I get the error "The left-hand side of an assignment must be a variable,
property or indexer". Since a.Amount is a property, I don't understand the
complaint. Is this supposed to work or not?

Have you *actually* got the code above, or have you got something like:

((MyType)a).Amount += 10;

?

Your code should work, and here's a simple test program to show it
working:

using System;

class Test
{
float amount=0;
public float Amount
{
get { return amount; }
set { amount = value; }
}

static void Main()
{
Test a = new Test();
a.Amount += 10;
}
}

Could you product a similar short but complete program that
demonstrates the error?
 
Jon Skeet said:
Have you *actually* got the code above, or have you got something like:

I found it. Account wasn't a class as I said, but a struct. I don't know
why, but I only needed to change "struct" to "class" to get it working. :-)
Thank you very much for the enlightening example anyway.

Gustaf
 
Back
Top