?: operator compared to if-else

G

GS

Can somebody explain why first part works and second part does not? Second operator throws error below

Error 52 Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'double'


if (myItem.BuyItNowPrice == null)
{
myRow["Buy It Now Price"] = null;
}
else
{
myRow["Buy It Now Price"] = myItem.BuyItNowPrice.Value;
}

myRow["Buy It Now Price"] = (myItem.BuyItNowPrice == null) ? null : myItem.BuyItNowPrice.Value; <-- Does not work
 
K

Kevin Spencer

The conditional expression you used needs to work with equivalent data
types, types that are either the same data type, or can be implicitly
converted to the same data type.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.

Can somebody explain why first part works and second part does not? Second
operator throws error below

Error 52 Type of conditional expression cannot be determined because there
is no implicit conversion between '<null>' and 'double'


if (myItem.BuyItNowPrice == null)
{
myRow["Buy It Now Price"] = null;
}
else
{
myRow["Buy It Now Price"] = myItem.BuyItNowPrice.Value;
}

myRow["Buy It Now Price"] = (myItem.BuyItNowPrice == null) ? null :
myItem.BuyItNowPrice.Value; <-- Does not work
 
K

Karl Seguin [MVP]

it has to do with the internal implementation of the operator. Your two statements are functionality equivilant, but that isn't actually what happens.

A local variable is created, to store the actual result. But since your result can be of two types it gets tripped up.

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Can somebody explain why first part works and second part does not? Second operator throws error below

Error 52 Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'double'


if (myItem.BuyItNowPrice == null)
{
myRow["Buy It Now Price"] = null;
}
else
{
myRow["Buy It Now Price"] = myItem.BuyItNowPrice.Value;
}

myRow["Buy It Now Price"] = (myItem.BuyItNowPrice == null) ? null : myItem.BuyItNowPrice.Value; <-- Does not work
 
Joined
May 18, 2007
Messages
1
Reaction score
0
One way to get around this if you want to use the ?: operator is to explicitly convert the null value to the value of your nullable type. For example:

Code:
DateTime? testDateTime;
string testData = '05/18/2007';

testDateTime= string.IsNullOrEmpty(testData) ? (DateTime?)null : DateTime.Parse(testData);

Obviously you have boxing goin on here so you need to take that into consideration when it comes to performance. The if...else statement will perform better because the boxing of null won't occur but this may not be of much concern depending upon your given scenario.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top