private void checkBox1_CheckedChanged(object sender, EventArgs e) { if(checked) { farmer = new Farmer(1, 400); } else { farmer = new Farmer(1, 300); } } 

Help
what am I doing wrong
error after (checked)

screenshots

enter image description here

enter image description here

Closed due to the fact that it was off topic by Nicolas Chabanovsky ♦ 3 Oct '16 at 6:04 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 2
    Apparently it should be if (checkBox1.Checked) for checked does not apply to anything - Alexey Shimansky
  • @ Alexey Shimansky ciab now plows - NoProgress
  • one
    @ Alexey Shimansky plus checked is a language keyword and in any case cannot be used as a variable / field name. Write the answer, please. - andreycha
  • @andreycha that the key even on the backlight can be seen. I didn’t write therefore .. I give you the right to answer)) - Alexey Shimansky
  • @ Alexey Shimansky, it was rather an addition to the author of the question. Do not be lazy :). - andreycha

1 answer 1

The IDE says that the error is for two reasons:

  1. checked is a keyword that is used to explicitly enable overflow checking when performing arithmetic operations and conversions with integer data. see the documentation . Therefore, it is already impossible to use it in if and there will be an error.
  2. Checkbox has a Checked property. I note that the property is written with a capital letter.

    That it must be checked. Naturally, it is necessary to specify an object at the beginning, and then a property of the object through a dot. You now have it if it were not a keyword, then it does not apply to anything. And you need to write at least checkBox1.Checked

    Because Does a property change this particular object? then you can cast the object that sent the event and check Checked with it, i.e.

     if (((CheckBox)sender).Checked) { ...