If statement in one line?

Mavix

Senior Member
Joined
Aug 14, 2006
Messages
759
Reaction score
3
Location
Durban
How do you write an if statement in one line in the C-type languages? I've been trying to figure it out the whole day, but I just can't remember how to do it.
All I can remember is that it has a question mark, a colon and brackets...
 
bool x = (y==b) ? true : false;

don't have a compiler.. but that is it...

Edit :

y and b is just variables.
 
Last edited:
You don't even need brackets... You can just test any boolean value, i.e.:

string results = 1 < 2 ? "yes" : "no";
 
you can even write all your code in one line - just don't use the enter key or formatting :eek:

inline is nice and effient, just remember some inlines tend to get hard to read.
 
I dunno, they work if they are in short lines of code. It gets a bit ridiculous if have a bout 5 or 6 inlines nested in one another. Then rather use a function to do the logic...
 
Inlines are extremely painful to follow. Avoid.

they have their uses for example

Code:
tblCourseSelect.Rows[3].Style.Value = (newCourse) ? "display:block" : "display:none"; //course type 
tblCourseSelect.Rows[4].Style.Value = (newCourse) ? "display:block" : "display:none"; //course date start
tblCourseSelect.Rows[5].Style.Value = (newCourse) ? "display:block" : "display:none"; //course date end

instead of

Code:
if (newCourse)
{
     tblCourseSelect.Rows[3].Style.Value "display:block"; //course type 
     tblCourseSelect.Rows[4].Style.Value "display:block"; //course date start 
     tblCourseSelect.Rows[5].Style.Value "display:block"; //course date end
}
else
{
     tblCourseSelect.Rows[3].Style.Value "display:none"; //course type 
     tblCourseSelect.Rows[4].Style.Value "display:none"; //course date start 
     tblCourseSelect.Rows[5].Style.Value "display:none"; //course date end
}

I dunno, they work if they are in short lines of code. It gets a bit ridiculous if have a bout 5 or 6 inlines nested in one another. Then rather use a function to do the logic...
exactly.
 
One such example of when to use this:
Code:
string x= "1";
bool test = x == "1" ? true : false;

I get this coming from databases quite a bit. No, I don't design said databases. :(
 
I dunno, they work if they are in short lines of code. It gets a bit ridiculous if have a bout 5 or 6 inlines nested in one another. Then rather use a function to do the logic...
OMW...

I had to take over a guy's code, a .net- expert according to him.

NO Functions... everything is just copied and pasted code (over and over and over), and everything (well 99%) is in the page_load event, no function calls. For the themes, he just copied the EXACT same 20 lines of code thru 20 aspx pages. Nice!! (Didn't think about using a baseclass , inheritence or anything...)

Yes... he is "an expert .net developer". He is the biggest idiot to walk this earth, luckily he left the company. Why I call him an idiot, when we tried to help him.. he told us to f-off.HE knows the best , because he has 20 years experience in development.

Edit: I typed like a drunken sailor
 
Last edited:
Hahaha! Funny stuff that... He didn't even have to use a base-class or inheritance. Just use Master Pages! That should take care of most "recurring themes" and code... :D
 
Hahaha! Funny stuff that... He didn't even have to use a base-class or inheritance. Just use Master Pages! That should take care of most "recurring themes" and code... :D
He did use master pages, but to set the theme on the pre_init, he copied and pasted the same code 1000000000 times. Well different ways to skin a cat, but this way was just insane.

I'll post the code but you might lose few IQ points :)

You must see it... scary stuff.
 
He did use master pages, but to set the theme on the pre_init, he copied and pasted the same code 1000000000 times. Well different ways to skin a cat, but this way was just insane.

I'll post the code but you might lose few IQ points :)

You must see it... scary stuff.

lol some start with bad code and progress along the lines, but others stay there professionally :p
 
Why not have a dedicated "Never code like this" thread to post the worst lines of (production) code you're ever seen? That should be a bit of a laugh! :D
 
Why not have a dedicated "Never code like this" thread to post the worst lines of (production) code you're ever seen? That should be a bit of a laugh! :D
I'm not the ultimate coder , make mistakes too. But some of the code I have seen will make you just go WTFX10000 and drop your IQ by 10%
 
Top
Sign up to the MyBroadband newsletter
X