Re: [C#] Whats wrong with my code?
Quote:
Originally Posted by
Qchan
That's wrong. It should be
Code:
if (checkBox1.Checked == true)
{
}
elseif(checkBox2.Enabled == true)
{
}
elseif(checkBox2.Enabled == false)
{
}
Not only is the code you have written incorrect syntax (there is no elseif in C#) and bloated (even if there was an elseif, the whole "elseif(checkBox2.Enabled == false)" line could be simplified to "else", you're making the assumptions that the OP
1 - wants to do something based on checkBox2's state, rather than setting checkBox2's state
2 - only wants to set something on checkBox2's state iff checkBox1 is unchecked.
Bearing in mind that the OP has already accepted a solution that involves setting the state of checkBox2 to the same state as checkBox1, neither of these assumptions hold true and the code shouldn't be what you said it "should be". It is clear that what he was trying to use was an assignment operator not an equality operator and thus his semi colons are also correct (though there are some useless parenthesis in there, probably from incorrect copying and pasting).
The only improvement that can be made on the accepted solution that gets the same result is tidying it up as I said in my previous post:
Code:
checkBox2.Enabled = checkBox1.Enabled;
Re: [C#] Whats wrong with my code?
Guys he posted working code already, there isn't anything wrong with this snippet anymore, and most of the stuff you guys posted i told him at school on the same day.