Solved

Type of conditional expression cannot be determined because there is no implicit convertion between null and int

Posted on 2009-05-11
3
414 Views
Last Modified: 2013-11-07
I have a few fields that I'm reading from an excel file that could be blank (null) or with values (int or decimal).

Below is the code I've written, but it seemed like the conditional statement would not work as there is a type mismatch between the true/false statement. Is there a way to convert to a nullable type in the false statement?

Thanks in advance.  
decimal? paid = dReader["PAID_" + dynColsSuffix] == null ? null : Convert.ToDecimal(dReader["PAID_" + dynColsSuffix]);

Open in new window

0
Comment
Question by:ngobw
3 Comments
 
LVL 3

Accepted Solution

by:
steeza earned 125 total points
ID: 24352615
The compiler is unable to determine the type of the null part of your statement.
I don't have a C# compiler handy and haven't used it in a while, but you should have 2 ways of dealing with this:

a) Cast your null to a decimal? Ie. (decimal?)null
b) Let the statement return a decimal such as 0.0 when the column is null.
0
 
LVL 12

Assisted Solution

by:GuitarRich
GuitarRich earned 125 total points
ID: 24352652
Try this - or you could write your own ToDecimalNullable function that returned a decimal?

decimal? paid = dReader["PAID_" + dynColsSuffix] == null ? null : (decimal?)dReader["PAID_" + dynColsSuffix];
 

Open in new window

0
 
LVL 9

Expert Comment

by:tculler
ID: 24354989
I think what the problem here is that index of dReader is returning a struct of some kind. A struct cannot be compared to null. What is the exact type of "dReader"?

The last part of your statement (the Convert.ToDecimal) is not your problem here--there is always an implicit conversion from a non-nullable into a nullable (just not vice versa -- use the Coalesce operator ( "??")).
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question