4 Replies - 707 Views - Last Post: 27 February 2010 - 03:34 AM Rate Topic: -----

#1 systemerror  Icon User is offline

  • D.I.C Head

Reputation: -20
  • View blog
  • Posts: 203
  • Joined: 15-August 09

C# If Statements

Posted 25 February 2010 - 03:26 AM

For a long time I have been writing my if statements like this

if (label1.text == "Woot")
{
label2.text =  "WooHoo"
}


Is there any type of code that will make it even simpler and easier and faster to type?
Is This A Good Question/Topic? 0
  • +

Replies To: C# If Statements

#2 remorseless  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 29
  • View blog
  • Posts: 129
  • Joined: 08-August 09

Re: C# If Statements

Posted 25 February 2010 - 03:41 AM

View Postsystemerror, on 25 February 2010 - 07:26 PM, said:

For a long time I have been writing my if statements like this

if (label1.text == "Woot")
{
label2.text =  "WooHoo"
}


Is there any type of code that will make it even simpler and easier and faster to type?


If it's just one thing to go through then you can cancel out the brackets. E.g:
if(label1.Text == "Woot") label2.Text = "WooHoo";



But, if it is more than one statement then you'll need to wrap them in braces e.g.
if(label1.Text == "Woot")
{
        label2.Text = "WooHoo";
        label3.Text = "Roflmao";
}


That is as compact as you can get..

Edit: Haha, oh wow. Me and Adkins are like twins xD

This post has been edited by remorseless: 25 February 2010 - 12:57 PM

Was This Post Helpful? 1
  • +
  • -

#3 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: C# If Statements

Posted 25 February 2010 - 03:42 AM

if (label1.text == "Woot")
label2.text = "WooHoo";



First off you forgot the semicolon at the end of label2.text :P Secondly, you don't need the brackets unless there is more than one line of code after the conditional. I always add them in simply to make it easier to expand them later on and for readability, but it isn't strictly required. I would have to say don't use if's like that, but the option is there if you so choose. I suppose you could simplify even more if your heart desires, but having it all on one line, but again that is going to get real ugly real quick.

EDIT: Remorseless beat me to it!

This post has been edited by Adkins: 25 February 2010 - 03:43 AM

Was This Post Helpful? 1
  • +
  • -

#4 keakTheGEEK  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 107
  • View blog
  • Posts: 344
  • Joined: 23-February 10

Re: C# If Statements

Posted 25 February 2010 - 01:56 PM

The ternary operator is an option as well where applicable:
int maxValue = value1 > value2 ? value1 : value2;



instead of:
int maxValue = 0;

if (value1 > value2)
    maxValue = value1;
else
    maxValue = value2;


This post has been edited by keakTheGEEK: 25 February 2010 - 01:58 PM

Was This Post Helpful? 1
  • +
  • -

#5 Vishu Sukhdev  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 150
  • Joined: 19-February 10

Re: C# If Statements

Posted 27 February 2010 - 03:34 AM

try this

if(lable1.text=="Woot")lable2.text="WooHoo";else lable2.text="HooWoo";



if(lable1.text=="Woot")
     lable2.text="WooHoo";
else 
    lable2.text="HooWoo";



if(lable1.text=="Woot")
{
     lable2.text="WooHoo";
     lable3.text="OOOOOO";
}
else 
{
    lable2.text="HooWoo";
    lable3.text="WWWWWW";
}


This post has been edited by eclipsed4utoo: 27 February 2010 - 06:19 AM
Reason for edit:: please use the code tags .... [code] your code here [/code]

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1