[C#] Whats wrong with my code?

Newbie Spellweaver
Joined
Jun 6, 2007
Messages
56
Reaction score
0
Hi,
I'm new to C# and just teaching my self how to code from exmaple off the internet etc.
I'm really bored so i thought i'd tre something out..

I'm stuck with this
if (checkBox1.Checked == true)
{
(checkBox2.Enabled == true);
}
else
{
(checkBox2.Enabled == false);
}

I get these errors with it
Error 1 Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\Users\Jack Parker\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 23 17 WindowsFormsApplication1
Error 2 Only assignment, call, increment, decrement, and new object expressions can be used as a statement C:\Users\Jack Parker\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 27 17 WindowsFormsApplication1

It's simple code but i'm new to C# and am stuck with it.

Thanks.
 
Box.Enabled = true;
The == is used only to compare, not to assign. You want to assign 'true' to Enabled attribute ;)
 
You don't to use () in if statements besides defining what you want to check.

Also,
== is used for reading
= is used for setting
Code:
if (checkBox1.Checked == true)
{
checkBox2.Enabled = true;
}
else
{
checkBox2.Enabled = false;
}
 
Hi,
I'm new to C# and just teaching my self how to code from exmaple off the internet etc.
I'm really bored so i thought i'd tre something out..

I'm stuck with this


I get these errors with it


It's simple code but i'm new to C# and am stuck with it.

Thanks.

Hi! C# follows the same syntax as C++ and Java in many respects. So, when it comes to nested if statements, they generally look the same.

You said

if (checkBox1.Checked == true)
{
(checkBox2.Enabled == true);
}
else
{
(checkBox2.Enabled == false);
}

That's wrong. It should be

if (checkBox1.Checked == true)
{
}
elseif(checkBox2.Enabled == true)
{
}
elseif(checkBox2.Enabled == false)
{
}

if statements don't need the semi colon (;). The semi colon is only needed for particular lines of code such as 'const string BARK = "Woof!";'

Also, WHILE and FOR statements also don't need a semi colon.
 
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;
 
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.
 
Back