Solved

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

Posted on 2009-05-11
3
405 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now