473,406 Members | 2,849 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

input == null ? "" : input

Hey

I've bought the book "ASP.NET 2.0 website programming, Problem, Design,
Solution" and some of its code examples is this code:

protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}

I have problem understanding this line: (input == null? "":input);
input shall only be set to "" if the value of input is NULL, but I don't see
any if-statement in this code. ?

Best Regards!

Jeff
Sep 13 '06 #1
2 2007
? : is a conditional statement. It is a standard statement in all C-based
languages.

--
Eliyahu Goldin,
Software Developer & Consultant
Microsoft MVP [ASP.NET]
"Jeff" <it************@hotmail.com.NOSPAMwrote in message
news:ew****************@TK2MSFTNGP02.phx.gbl...
Hey

I've bought the book "ASP.NET 2.0 website programming, Problem, Design,
Solution" and some of its code examples is this code:

protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}

I have problem understanding this line: (input == null? "":input);
input shall only be set to "" if the value of input is NULL, but I don't
see any if-statement in this code. ?

Best Regards!

Jeff


Sep 13 '06 #2
Hi,

Jeff wrote:
Hey

I've bought the book "ASP.NET 2.0 website programming, Problem, Design,
Solution" and some of its code examples is this code:

protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}

I have problem understanding this line: (input == null? "":input);
input shall only be set to "" if the value of input is NULL, but I don't see
any if-statement in this code. ?

Best Regards!

Jeff
The '?' conditional statement evaluates the first term (after the '?'
and before the ':') if the condition is true, and evaluates the second
term (after the ':') if the condition is false.

In that case, the code ensures that the "null" value is never returned,
but only an empty string. Translated to an "if" expression, it would be:

if ( input == null )
{
return "";
}
else
{
return input;
}

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Sep 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Don Vaillancourt | last post by:
What's going on with Javascript. At the beginning there was the "undefined" value which represented an object which really didn't exist then came the null keyword. But yesterday I stumbled...
13
by: gary | last post by:
Hi, We all know the below codes are dangerous: { int *p = new int; delete p; delete p; } And we also know the compilers do not delete p if p==NULL. So why compilers do not "p = NULL"...
14
by: MuZZy | last post by:
Hi, Lately i've been (and still am) fixing some memory leaks problems in the project i just took over when i got this new job. Among the other issues i've noticed that for localy created objects...
4
by: Peter Hemmingsen | last post by:
Hi, I have a dotnet object (implemented in mc++ and used in c#) which have a property called "Info". The Info property is also a dotnet object (implemented in mc++). In the constructor of the...
8
by: craigkenisston | last post by:
I have a generic function that receives a couple of datetime values to work with. They can or cannot have a value, therefore I wanted to use null. This function will call a database stored...
5
RMWChaos
by: RMWChaos | last post by:
I am working on a script to create and remove DOM elements, and I want to make it as efficient as possible (no redundancies). Because DOM elements each have their own set of attributes, the function...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.