?: operator compared to if-else

  • Thread starter Thread starter GS
  • Start date Start date
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
 
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
 
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
 

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

Back
Top