Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'if' statements

Tags:

c#

winforms

Im still new to C# and was wondering how one would have multiple things happen when an if condition is met. for example.

int number = (Convert.ToInt32(textbox1.text)); 
   if (number == 1)
         textbox2.Text = "1";                 
         number2 = 33;           
         textbox3.text = (Convert.ToString(number2));

When I do something like this it dosnt complete all the desired results.

P.S If this isnt the right site to go for newbie questions like this does anyone know where I can go? (after research of course).

like image 874
Joe Fredy Avatar asked Jul 14 '12 20:07

Joe Fredy


2 Answers

if (number == 1) 
{
    textbox2.Text = "1";                 
    number2 = 33;           
    textbox3.text = (Convert.ToString(RSP));
}

Add brackets to group statements together.

Without the brackets, the if-statement will ONLY affect the very next statement: textbox2.Text = "1";, and the other statements will always be run, regardless of the if-statement.

like image 158
abelenky Avatar answered Nov 14 '22 20:11

abelenky


You need to use a block, using the { and } characters...

int number = (Convert.ToInt32(textbox1.text)); 
if (number == 1)
{
    textbox2.Text = "1";                 
    number2 = 33;           
    textbox3.text = (Convert.ToString(RSP));
}
like image 2
freefaller Avatar answered Nov 14 '22 21:11

freefaller