Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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 …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

948 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

23 Experts available now in Live!

Get 1:1 Help Now